home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / variable.txt < prev    next >
Text File  |  1994-09-21  |  92KB  |  2,314 lines

  1.  
  2.          WHAT'S A VARIABLE?
  3.   A letter can stand for a number. For example, X can stand for 
  4. the number 47, as in this program:
  5. 10 X=47
  6. 20 PRINT X+2
  7.   Line 10 says X stands for the number 47. In other words, X is a 
  8. name for the number 47.
  9.   Line 20 says to print X+2. Since X is 47, X+2 is 49; so the 
  10. computer will print 49. That's the only number the computer will 
  11. print; it will not print 47.
  12.  
  13.                Jargon
  14.   A letter that stands for a number is called a numeric variable. 
  15. In that program, X is a numeric variable; it stands for the 
  16. number 47. The value of X is 47. Line 10 is called an assignment 
  17. statement, because it assigns 47 to X.
  18.  
  19.             More examples
  20.   Here's another example:
  21. 10 Y=38
  22. 20 PRINT Y-2
  23.   Line 10 says Y is a numeric variable that stands for the number 
  24. 38.
  25.   Line 20 says to print Y-2. Since Y is 38, Y-2 is 36; so the 
  26. computer will print 36.
  27.   Example:
  28. 10 B=8
  29. 20 PRINT B*3
  30. Line 10 says B is 8. Line 20 says to print B*3, which is 8*3, 
  31. which is 24; so the computer will print 24.
  32.   One variable can define another:
  33. 10 M=6
  34. 20 P=M+1
  35. 30 PRINT M*P
  36. Line 10 says M is 6. Line 20 says P is M+1, which is 6+1, which 
  37. is 7; so P is 7. Line 30 says to print M*P, which is 6*7, which 
  38. is 42; so the computer will print 42.
  39.   A value can change:
  40. 10 F=4
  41. 20 F=9
  42. 30 PRINT F*2
  43. Line 10 says F's value is 4. Line 20 changes F's value to 9, so 
  44. line 30 prints 18.
  45.                                                       Hassles
  46.                                          On the left side of the 
  47. equal sign, you must have one variable:
  48. Allowed                                            Not allowed     
  49. Not allowed
  50. P=M+1                                              P-M=1           
  51. 1=P-M
  52.                                                                 
  53. one variable                                       two variables   
  54. not a variable
  55.                                          The variable on the left 
  56. side of the equation is the only one that changes. For example, 
  57. the statement P=M+1 changes the value of P but not M. The 
  58. statement A=B changes the value of A but not B:
  59. 10 A=1
  60. 20 B=7
  61. 30 A=B
  62. 40 PRINT A+B
  63. Line 30 changes A, to make it equal B; so A becomes 7. Since both 
  64. A and B are now 7, line 40 prints 14.
  65.                                          ``A=B'' versus ``B=A'' 
  66. Saying ``A=B'' has a different effect from ``B=A''. That's 
  67. because ``A=B'' changes the value of A (but not B); ``B=A'' 
  68. changes the value of B (but not A).
  69.                                          Compare these programs:
  70. 10 A=1                10 A=1
  71. 20 B=7                20 B=7
  72. 30 A=B                30 B=A
  73. 40 PRINT A+B          40 PRINT A+B
  74.                                          In the left program 
  75. (which you saw before), line 30 changes A to 7, so both A and B 
  76. are 7. Line 40 prints 14.
  77.                                          In the right program, 
  78. line 30 changes B to 1, so both A and B are 1. Line 40 prints 2.
  79.    A variable is a box
  80.   If variables ever confuse you, think of a variable as being a 
  81. box. Here's how. . . . 
  82.   The computer's random-access memory (RAM) consists of 
  83. electronic boxes. This program puts a number into a box:
  84. 10 X=47
  85. 20 PRINT X+2
  86. Line 10 puts 47 into box X, like this:
  87.        ┌─────────────┐
  88. Box X  │       47    │
  89.        └─────────────┘
  90. Line 20 says to print what's in box X, plus 2. So the computer 
  91. will print 49.
  92.   You can change what's in a box:
  93. 10 F=4
  94. 20 F=9
  95. 30 PRINT F*2
  96. Line 10 puts 4 into box F:
  97.        ┌─────────────┐
  98. Box F  │        4    │
  99.        └─────────────┘
  100. Line 20 puts 9 into box F; the 9 replaces the 4:
  101.        ┌─────────────┐
  102. Box F  │        9    │
  103.        └─────────────┘
  104. Line 30 prints 18.
  105.   To see why ``A=B'' has a different effect from ``B=A'', compare 
  106. these programs again:
  107. 10 A=1                10 A=1
  108. 20 B=7                20 B=7
  109. 30 A=B                30 B=A
  110. 40 PRINT A+B          40 PRINT A+B
  111. In both programs, lines 10 and 20 do this:
  112.        ┌─────────────┐
  113. Box A  │        1    │
  114.        └─────────────┘
  115.        ┌─────────────┐
  116. Box B  │        7    │
  117.        └─────────────┘
  118. In the left program, line 30 makes the number in box A become 7 
  119. (so both boxes contain 7, and line 40 prints 14). In the right 
  120. program, line 30 makes the number in box B become 1 (so both 
  121. boxes contain 1, and line 40 prints 2).
  122.                                          When to use variables
  123.                              Here's a practical example of when 
  124. to use variables.
  125.                              Suppose you're selling something 
  126. that costs $1297.43, and you want to do these calculations:
  127. multiply $1297.43 by 2
  128. multiply $1297.43 by .05
  129. add $1297.43 to $483.19
  130. divide $1297.43 by 37
  131. subtract $1297.43 from $8598.61
  132. multiply $1297.43 by 28.7
  133.                              To do those six calculations, you 
  134. could run this program:
  135. 10 PRINT 1297.43*2; 1297.43*.05; 1297.43+483.19; 1297.43/37; 
  136. 8598.61-1297.43
  137. 20 PRINT 1297.43*28.7
  138. But that program's silly, since it contains the number 1297.43 
  139. six times. This program's briefer, because it uses a variable:
  140. 10 C=1297.43
  141. 20 PRINT C*2; C*.05; C+483.19; C/37; 8598.61-C; C*28.7
  142.                              So whenever you need to use a number 
  143. several times, turn the number into a variable, which will make 
  144. your program briefer.
  145.  
  146.                                            String variables
  147.                              A string is any collection of 
  148. characters, such as ``I LOVE YOU''. Each string must be in 
  149. quotation marks.
  150.                              A letter can stand for a string ___ 
  151. if you put a dollar sign after the letter, like this:
  152. 10 G$="DOWN"
  153. 20 PRINT G$
  154.                              Line 10 says G$ stands for the 
  155. string ``DOWN''. Line 20 prints:
  156. DOWN
  157.                              In that program, G$ is a variable. 
  158. Since it stands for a string, it's called a string variable.
  159.                              Every string variable must end with 
  160. a dollar sign. The dollar sign is supposed to remind you of a 
  161. fancy S, which stands for String. Line 10 is pronounced, ``G 
  162. String is DOWN''.
  163.                              If you're paranoid, you'll love this 
  164. program:
  165. 10 L$="THEY'RE LAUGHING AT YOU"
  166. 20 PRINT L$
  167. 30 PRINT L$
  168. 40 PRINT L$
  169. Line 10 says L$ stands for the string ``THEY'RE LAUGHING AT 
  170. YOU''. Lines 20, 30, and 40 make the computer print:
  171. THEY'RE LAUGHING AT YOU
  172. THEY'RE LAUGHING AT YOU
  173. THEY'RE LAUGHING AT YOU
  174.  
  175.                                             Nursery rhymes
  176.                              The computer can recite nursery 
  177. rhymes:
  178. 10 P$="PEAS PORRIDGE"
  179. 20 PRINT P$;" HOT"
  180. 30 PRINT P$;" COLD"
  181. 40 PRINT P$;" IN THE POT"
  182. 50 PRINT "NINE DAYS OLD"
  183.                              Line 10 says P$ stands for ``PEAS 
  184. PORRIDGE''. Lines 20-50 make the computer print:
  185. PEAS PORRIDGE HOT
  186. PEAS PORRIDGE COLD
  187. PEAS PORRIDGE IN THE POT
  188. NINE DAYS OLD
  189.  
  190.   This program prints a fancier rhyme:
  191. 10 H$="HICKORY, DICKORY, DOCK!"
  192. 20 M$="THE MOUSE (SQUEAK! SQUEAK!)"
  193. 30 C$="THE CLOCK (TICK! TOCK!)"
  194. 40 PRINT H$
  195. 50 PRINT M$;" RAN UP ";C$
  196. 60 PRINT C$;" STRUCK ONE"
  197. 70 PRINT M$;" RAN DOWN"
  198. 80 PRINT H$
  199. Lines 10-30 define H$, M$, and C$. Lines 40-80 make the computer 
  200. print:
  201. HICKORY, DICKORY, DOCK!
  202. THE MOUSE (SQUEAK! SQUEAK!) RAN UP THE CLOCK (TICK! TOCK!)
  203. THE CLOCK (TICK! TOCK!) STRUCK ONE
  204. THE MOUSE (SQUEAK! SQUEAK!) RAN DOWN
  205. HICKORY, DICKORY, DOCK!
  206.  
  207.                Undefined variables
  208.   If you don't define a numeric variable, the computer assumes 
  209. it's zero:
  210. 10 PRINT R
  211. Since R hasn't been defined, line 10 prints zero.
  212.   The computer doesn't look ahead:
  213. 10 PRINT J
  214. 20 J=5
  215. When the computer encounters line 10, it doesn't look ahead to 
  216. find out what J is. As of line 10, J is still undefined, so the 
  217. computer prints zero.
  218.   If you don't define a string variable, the computer assumes 
  219. it's blank:
  220. 10 PRINT F$
  221. Since F$ hasn't been defined, line 10 makes the computer print a 
  222. line that says nothing; the line the computer prints is blank.
  223.  
  224.                Long variable names
  225.   A numeric variable's name can be a letter (such as X) or a 
  226. longer combination of characters, such as:
  227. PROFIT.IN.1989.BEFORE.NOVEMBER.PROMOTION
  228.   For example, you can type:
  229. 10 PROFIT.IN.1989.BEFORE.NOVEMBER.PROMOTION = 3497.18
  230. 20 PROFIT.IN.1989 = PROFIT.IN.1989.BEFORE.NOVEMBER.PROMOTION + 
  231. 6214.27
  232. 30 PRINT PROFIT.IN.1989
  233. The computer will print:
  234.  9711.45
  235.   The variable's name can be quite long: up to 40 characters!
  236.   The first character in the name must be a letter. The remaining 
  237. characters can be letters, digits, or periods.
  238.   The name must not be a word that has a special meaning to the 
  239. computer. For example, the name cannot be PRINT.
  240.   If the variable stands for a string, the name can have up to 40 
  241. characters, followed by a dollar sign, making a total of 41 
  242. characters, like this:
  243. MY.JOB.IN.1989.BEFORE.NOVEMBER.PROMOTION$
  244.   Although modern computers permit long variable names, primitive 
  245. computers don't. To find out whether your computer permits long 
  246. variable names, check the ``Versions of BASIC'' appendix.
  247.   Professional programmers use long variable names, because long 
  248. names make the programs easier to understand.
  249.   I'll avoid long names since some computers don't permit them; 
  250. but if your computer permits them, use them!
  251.  
  252.  
  253.                       INPUT
  254.   Humans ask questions; so to turn the computer into a human, you 
  255. must make it ask questions too. To make the computer ask a 
  256. question, use the word INPUT.
  257.   This program makes the computer ask for your name:
  258. 10 INPUT "WHAT IS YOUR NAME";N$
  259. 20 PRINT "I ADORE ANYONE WHOSE NAME IS ";N$
  260.   When the computer sees line 10, the computer asks ``WHAT IS 
  261. YOUR NAME?'' and then waits for you to answer the question. Your 
  262. answer will be called N$. For example, if you answer MARIA, then 
  263. N$ is MARIA. Line 20 makes the computer print:
  264. I ADORE ANYONE WHOSE NAME IS MARIA
  265.   Here's the whole conversation; I've underlined the parts typed 
  266. by you. . . . 
  267. You tell the computer to run:RUN
  268. The computer asks for your name:WHAT IS YOUR NAME? MARIA
  269. The computer praises your name:I ADORE ANYONE WHOSE NAME IS MARIA
  270.   Try that example. Be careful! When you type line 10, which says 
  271. INPUT, make sure you type the two quotation marks and the 
  272. semicolon. You don't have to type a question mark: when the 
  273. computer runs your program, it will automatically put a question 
  274. mark at the end of the question.
  275.   Just for fun, run that program again and pretend you're 
  276. somebody else. . . . 
  277. You tell the computer to run:RUN
  278. The computer asks for your name:WHAT IS YOUR NAME? BUD
  279. The computer praises your name:I ADORE ANYONE WHOSE NAME IS BUD
  280.   When the computer asks for your name, if you say something 
  281. weird, the computer will give you a weird reply. . . . 
  282. Make the computer run:RUN
  283. Computer asks your name:WHAT IS YOUR NAME? NONE OF YOUR 
  284. BUSINESS!!!
  285. The computer replies:I ADORE ANYONE WHOSE NAME IS NONE OF YOUR 
  286. BUSINESS!!!
  287.  
  288.                College admissions
  289.   This program prints a letter, admitting you to the college of 
  290. your choice:
  291. 10 INPUT "WHAT COLLEGE WOULD YOU LIKE TO ENTER";C$
  292. 20 PRINT "CONGRATULATIONS!"
  293. 30 PRINT "YOU HAVE JUST BEEN ADMITTED TO ";C$
  294. 40 PRINT "IT FITS YOUR PERSONALITY"
  295. 50 PRINT "I HOPE YOU GO TO ";C$
  296. 60 PRINT "           RESPECTFULLY YOURS"
  297. 70 PRINT "           THE DEAN OF ADMISSIONS"
  298.   When the computer sees line 10, the computer asks ``WHAT 
  299. COLLEGE WOULD YOU LIKE TO ENTER?'' and waits for you to answer. 
  300. Your answer will be called C$. If you'd like to be admitted to 
  301. HARVARD, you'll be pleased. . . . 
  302. You tell the computer to run:RUN
  303. The computer asks:WHAT COLLEGE WOULD YOU LIKE TO ENTER? HARVARD
  304. The computer admits you:CONGRATULATIONS!
  305.                 YOU HAVE JUST BEEN ADMITTED TO HARVARD
  306.                 IT FITS YOUR PERSONALITY
  307.                 I HOPE YOU GO TO HARVARD
  308.                 RESPECTFULLY YOURS
  309.                 THE DEAN OF ADMISSIONS
  310.   You can choose any college you wish:
  311. RUN
  312. WHAT COLLEGE WOULD YOU LIKE TO ENTER? HELL
  313. CONGRATULATIONS!
  314. YOU HAVE JUST BEEN ADMITTED TO HELL
  315. IT FITS YOUR PERSONALITY
  316. I HOPE YOU GO TO HELL
  317.            RESPECTFULLY YOURS
  318.            THE DEAN OF ADMISSIONS
  319.  
  320.                                                      That program 
  321. consists of three parts:
  322.                                                      1. The 
  323. computer begins by asking you a question (``What college would 
  324. you like to enter?''). The computer's question is called the 
  325. prompt, because it prompts you to answer.
  326.                                                      2. Your 
  327. answer (the college's name) is called your input, because it's 
  328. information that you're putting into the computer.
  329.                                                      3. The 
  330. computer's reply (the admission letter) is called the computer's 
  331. output, because it's the final answer that the computer puts out.
  332.  
  333.                                                       INPUT versus PRINT
  334.                                                      The word 
  335. INPUT is the opposite of the word PRINT.
  336.                                                      The word 
  337. PRINT makes the computer print information out. The word INPUT 
  338. makes the computer take information in.
  339.                                                      What the 
  340. computer prints out is called the output. What the computer takes 
  341. in is called your input.
  342.                                                      Input and 
  343. Output are collectively called I/O, so the INPUT and PRINT 
  344. statements are called I/O statements.
  345.                 Once upon a time
  346.   Let's make the computer write a story, by filling in the 
  347. blanks:
  348. ONCE UPON A TIME, THERE WAS A YOUNGSTER NAMED _____________
  349.                                                 your name
  350.  
  351. WHO HAD A FRIEND NAMED _________________
  352.                          friend's name
  353.  
  354. _____________ WANTED TO ________________________ 
  355. _________________
  356.   your name               verb (such as "pat")     friend's name
  357.  
  358. BUT _________________ DIDN'T WANT TO ________________________ 
  359. _____________
  360.       friend's name                    verb (such as "pat")     
  361. your name
  362.  
  363. WILL _____________ _______________________ _________________
  364.        your name     verb (such as "pat")    friend's name
  365.  
  366. WILL _________________ ________________________ _____________
  367.        friend's name     verb (such as "pat")     your name
  368.  
  369. TO FIND OUT, COME BACK AND SEE THE NEXT EXCITING EPISODE
  370.  
  371.  
  372. OF _____________ AND _________________
  373.      your name         friend's name
  374.   To write the story, the computer must ask for your name, your 
  375. friend's name, and a verb. To make the computer ask, your program 
  376. must say INPUT:
  377. 10 INPUT "WHAT IS YOUR NAME";Y$
  378. 20 INPUT "WHAT'S YOUR FRIEND'S NAME";F$
  379. 30 INPUT "IN 1 WORD, SAY SOMETHING YOU CAN DO TO YOUR FRIEND";V$
  380. Then make the computer print the story:
  381. 40 PRINT "HERE'S MY STORY...."
  382. 50 PRINT "ONCE UPON A TIME, THERE WAS A YOUNGSTER NAMED ";Y$
  383. 60 PRINT "WHO HAD A FRIEND NAMED ";F$
  384. 70 PRINT Y$;" WANTED TO ";V$;" ";F$
  385. 80 PRINT "BUT ";F$;" DIDN'T WANT TO ";V$;" ";Y$
  386. 90 PRINT "WILL ";Y$;" ";V$;" ";F$
  387. 100 PRINT "WILL ";F$;" ";V$;" ";Y$
  388. 110 PRINT "TO FIND OUT, COME BACK AND SEE THE NEXT EXCITING 
  389. EPISODE"
  390. 120 PRINT "OF ";Y$;" AND ";F$
  391.   Here's a sample run:
  392. WHAT'S YOUR NAME? DRACULA
  393. WHAT'S YOUR FRIEND'S NAME? MARILYN MONROE
  394. IN 1 WORD, SAY SOMETHING YOU CAN DO TO YOUR FRIEND? BITE
  395. HERE'S MY STORY....
  396. ONCE UPON A TIME, THERE WAS A YOUNGSTER NAMED DRACULA
  397. WHO HAD A FRIEND NAMED MARILYN MONROE
  398. DRACULA WANTED TO BITE MARILYN MONROE
  399. BUT MARILYN MONROE DIDN'T WANT TO BITE DRACULA
  400. WILL DRACULA BITE MARILYN MONROE
  401. WILL MARILYN MONROE BITE DRACULA
  402. TO FIND OUT, COME BACK AND SEE THE NEXT EXCITING EPISODE
  403. OF DRACULA AND MARILYN MONROE
  404.   Here's another run:
  405. WHAT'S YOUR NAME? SUPERMAN
  406. WHAT'S YOUR FRIEND'S NAME? KING KONG
  407. IN 1 WORD, SAY SOMETHING YOU CAN DO TO YOUR FRIEND? TICKLE
  408. HERE'S MY STORY....
  409. ONCE UPON A TIME, THERE WAS A YOUNGSTER NAMED SUPERMAN
  410. WHO HAD A FRIEND NAMED KING KONG
  411. SUPERMAN WANTED TO TICKLE KING KONG
  412. BUT KING KONG DIDN'T WANT TO TICKLE SUPERMAN
  413. WILL SUPERMAN TICKLE KING KONG
  414. WILL KING KONG TICKLE SUPERMAN
  415. TO FIND OUT, COME BACK AND SEE THE NEXT EXCITING EPISODE
  416. OF SUPERMAN AND KING KONG
  417.   Try it: put in your own name, the name of your friend, and 
  418. something you'd like to do to your friend.
  419.  
  420.                      Contest
  421.   This program prints a certificate saying you won a contest:
  422. 10 INPUT "WHAT'S YOUR NAME";Y$
  423. 20 INPUT "WHAT'S YOUR FRIEND'S NAME";F$
  424. 30 INPUT "WHAT'S THE NAME OF ANOTHER FRIEND";A$
  425. 40 INPUT "NAME A COLOR";C$
  426. 50 INPUT "NAME A PLACE";P$
  427. 60 INPUT "NAME A FOOD";D$
  428. 70 INPUT "NAME AN OBJECT";J$
  429. 80 INPUT "NAME A PART OF THE BODY";B$
  430. 90 INPUT "NAME A STYLE OF COOKING (SUCH AS BAKED OR FRIED)";S$
  431. 100 PRINT
  432. 110 PRINT "CONGRATULATIONS ";Y$
  433. 120 PRINT "YOU'VE WON THE BEAUTY CONTEST, BECAUSE OF YOUR 
  434. GORGEOUS ";B$
  435. 130 PRINT "YOUR PRIZE IS A ";C$;" ";J$
  436. 140 PRINT "PLUS A TRIP TO ";P$;" WITH YOUR FRIEND ";F$
  437. 150 PRINT "PLUS...AND THIS IS THE BEST PART OF ALL..."
  438. 160 PRINT "DINNER FOR THE TWO OF YOU AT ";A$;"'S NEW RESTAURANT"
  439. 170 PRINT "WHERE ";A$;" WILL GIVE YOU ALL THE ";S$;" ";D$;" YOU 
  440. CAN EAT"
  441. 180 PRINT "CONGRATULATIONS ";Y$;"...TODAY'S YOUR LUCKY DAY..."
  442. 190 PRINT "NOW EVERYBODY WANTS TO KISS YOUR AWARD-WINNING ";B$
  443.   Here's a sample run:
  444. WHAT'S YOUR NAME? LONG JOHN SILVER
  445. WHAT'S YOUR FRIEND'S NAME? THE PARROT
  446. WHAT'S THE NAME OF ANOTHER FRIEND? JIM
  447. NAME A COLOR? GOLD
  448. NAME A PLACE? TREASURE ISLAND
  449. NAME A FOOD? RUM-SOAKED COCONUTS
  450. NAME AN OBJECT? CHEST OF JEWELS
  451. NAME A PART OF THE BODY? MISSING LEG
  452. NAME A STYLE OF COOKING (SUCH AS BAKED OR FRIED)? BARBECUED
  453.  
  454. CONGRATULATIONS LONG JOHN SILVER
  455. YOU'VE WON THE BEAUTY CONTEST, BECAUSE OF YOUR GORGEOUS MISSING 
  456. LEG
  457. YOUR PRIZE IS A GOLD CHEST OF JEWELS
  458. PLUS A TRIP TO TREASURE ISLAND WITH YOUR FRIEND THE PARROT
  459. PLUS...AND THIS IS THE BEST PART OF ALL...
  460. DINNER FOR THE TWO OF YOU AT JIM'S NEW RESTAURANT
  461. WHERE JIM WILL GIVE YOU ALL THE BARBECUED RUM-SOAKED COCONUTS YOU 
  462. CAN EAT
  463. CONGRATULATIONS LONG JOHN SILVER...TODAY'S YOUR LUCKY DAY...
  464. NOW EVERYBODY WANTS TO KISS YOUR AWARD-WINNING MISSING LEG
  465.   This run describes the contest that brought Ronald Reagan to 
  466. the White House:
  467. WHAT'S YOUR NAME? RONNIE REAGAN
  468. WHAT'S YOUR FRIEND'S NAME? NANCY
  469. WHAT'S THE NAME OF ANOTHER FRIEND? ALICE
  470. NAME A COLOR? RED-WHITE-AND-BLUE
  471. NAME A PLACE? THE WHITE HOUSE
  472. NAME A FOOD? JELLY BEANS
  473. NAME AN OBJECT? COWBOY HAT
  474. NAME A PART OF THE BODY? CHEEKS
  475. NAME A STYLE OF COOKING (SUCH AS BAKED OR FRIED)? STEAMED
  476.  
  477. CONGRATULATIONS RONNIE REAGAN
  478. YOU'VE WON THE BEAUTY CONTEST, BECAUSE OF YOUR GORGEOUS CHEEKS
  479. YOUR PRIZE IS A RED-WHITE-AND-BLUE COWBOY HAT
  480. PLUS A TRIP TO THE WHITE HOUSE WITH YOUR FRIEND NANCY
  481. PLUS...AND THIS IS THE BEST PART OF ALL...
  482. DINNER FOR THE TWO OF YOU AT ALICE'S NEW RESTAURANT
  483. WHERE ALICE WILL GIVE YOU ALL THE STEAMED JELLY BEANS YOU CAN EAT
  484. CONGRATULATIONS RONNIE REAGAN...TODAY'S YOUR LUCKY DAY...
  485. NOW EVERYBODY WANTS TO KISS YOUR AWARD-WINNING CHEEKS
  486.  
  487.  
  488.                 Bills
  489.   If you're a nasty bill collector, you'll love this program:
  490. 10 INPUT "WHAT IS THE CUSTOMER'S FIRST NAME";F$
  491. 20 INPUT "LAST NAME";L$
  492. 30 INPUT "STREET ADDRESS";A$
  493. 40 INPUT "CITY";C$
  494. 50 INPUT "STATE";S$
  495. 60 INPUT "ZIP CODE";Z$
  496. 70 PRINT
  497. 80 PRINT F$;" ";L$
  498. 90 PRINT A$
  499. 100 PRINT C$;" ";S$;" ";Z$
  500. 110 PRINT
  501. 120 PRINT "DEAR ";F$;","
  502. 130 PRINT "   YOU STILL HAVEN'T PAID THE BILL."
  503. 140 PRINT "IF YOU DON'T PAY IT SOON, ";F$;","
  504. 150 PRINT "I'LL COME VISIT YOU IN ";C$
  505. 160 PRINT "AND PERSONALLY SHOOT YOU."
  506. 170 PRINT "            YOURS TRULY,"
  507. 180 PRINT "            SURE-AS-SHOOTIN'"
  508. 190 PRINT "            YOUR CRAZY CREDITOR"
  509.   Can you figure out what that program does?
  510.  
  511.             Numeric input
  512.   This program makes the computer predict your future:
  513. 10 PRINT "I PREDICT WHAT'LL HAPPEN TO YOU IN THE YEAR 2000!"
  514. 20 INPUT "IN WHAT YEAR WERE YOU BORN";Y
  515. 30 PRINT "IN THE YEAR 2000, YOU'LL TURN";2000-Y;"YEARS OLD."
  516.   Here's a sample run:
  517. I PREDICT WHAT'LL HAPPEN TO YOU IN THE YEAR 2000!
  518. IN WHAT YEAR WERE YOU BORN? 1962
  519. IN THE YEAR 2000, YOU'LL TURN 38 YEARS OLD.
  520.   Suppose you're selling tickets to a play. Each ticket costs 
  521. $2.79. (You decided $2.79 would be a nifty price, because the 
  522. cast has 279 people.) This program finds the price of multiple 
  523. tickets:
  524. 10 INPUT "HOW MANY TICKETS";T
  525. 20 PRINT "THE TOTAL PRICE IS $";T*2.79
  526.   This program tells you how much the ``energy crisis'' costs 
  527. you, when you drive your car:
  528. 10 INPUT "HOW MANY MILES DO YOU WANT TO DRIVE";M
  529. 20 INPUT "HOW MANY PENNIES DOES A GALLON OF GAS COST";P
  530. 30 INPUT "HOW MANY MILES-PER-GALLON DOES YOUR CAR GET";R
  531. 40 PRINT "THE GAS FOR YOUR TRIP WILL COST YOU $";M*P/(R*100)
  532. Here's a sample run:
  533. HOW MANY MILES DO YOU WANT TO DRIVE? 400
  534. HOW MANY PENNIES DOES A GALLON OF GAS COST? 95.9
  535. HOW MANY MILES-PER-GALLON DOES YOUR CAR GET? 31
  536. THE GAS FOR YOUR TRIP WILL COST YOU $ 12.3742
  537.  
  538.                                                     Conversion
  539.                                          This program converts 
  540. feet to inches:
  541. 10 INPUT "HOW MANY FEET";F
  542. 20 PRINT F;"FEET =";F*12;"INCHES"
  543.                                          Here's a sample run:
  544. HOW MANY FEET? 3
  545.  3 FEET = 36 INCHES
  546.                                          Trying to convert to the 
  547. metric system? This program converts inches to centimeters:
  548. 10 INPUT "HOW MANY INCHES";I
  549. 20 PRINT I;"INCHES =";I*2.54;"CENTIMETERS"
  550.                                          Nice day today, isn't 
  551. it? This program converts the temperature from Celsius to 
  552. Fahrenheit:
  553. 10 INPUT "HOW MANY DEGREES CELSIUS";C
  554. 20 PRINT C;"DEGREES CELSIUS =";C*1.8+32;"DEGREES FAHRENHEIT"
  555. Here's a sample run:
  556. HOW MANY DEGREES CELSIUS? 20
  557.  20 DEGREES CELSIUS = 68 DEGREES FAHRENHEIT
  558.                                          See, you can write the 
  559. Guide yourself! Just hunt through any old math or science book, 
  560. find any old formula (such as F=C*1.8+32), and turn it into a 
  561. program.
  562.  
  563.             IF . . . THEN
  564.   The computer understands the words IF and THEN.
  565.  
  566.               Therapist
  567.   Let's turn your computer into a therapist.
  568.   To make the computer ask the patient, ``HOW ARE YOU?'', begin 
  569. the program like this:
  570. 10 INPUT "HOW ARE YOU";A$
  571. That line makes the computer ask HOW ARE YOU and wait for the 
  572. patient's answer, which is called A$.
  573.   If the patient feels FINE, let's make the computer say THAT'S 
  574. GOOD. Here's how:
  575. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD"
  576. That line says that if the patient's answer (A$) is FINE, the 
  577. computer will print THAT'S GOOD.
  578.   If the patient feels LOUSY instead, let's make the computer say 
  579. TOO BAD. Here's how:
  580. 30 IF A$="LOUSY" THEN PRINT "TOO BAD"
  581.   Here's the entire program:
  582. 10 INPUT "HOW ARE YOU";A$
  583. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD"
  584. 30 IF A$="LOUSY" THEN PRINT "TOO BAD"
  585.   When the patient types RUN, line 10 makes the computer ask HOW 
  586. ARE YOU and wait for the patient's answer, which is called A$. If 
  587. the patient's answer is FINE, line 20 makes the computer reply 
  588. THAT'S GOOD. If the patient's answer is LOUSY, line 30 makes the 
  589. computer reply TOO BAD.
  590.   Try running that program! Here's a sample run:
  591. RUN
  592. HOW ARE YOU? FINE
  593. THAT'S GOOD
  594.   Here's another run:
  595. RUN
  596. HOW ARE YOU? LOUSY
  597. TOO BAD
  598.   Underlying theory Whenever you say IF, you should also say 
  599. THEN. For example, line 20 (which says IF) also says THEN. 
  600. Similarly, line 30 (which says IF) says THEN.
  601.   Do not put a comma before THEN.
  602.   What comes between IF and THEN is called the condition. In line 
  603. 20, the condition is:
  604. A$="FINE"
  605. If that condition is true (if A$ really does equal ``FINE''), the 
  606. computer does what comes after the word THEN, which is called the 
  607. consequence, which is:
  608. PRINT "THAT'S GOOD"
  609.   Increase the computer's vocabulary In that program, what 
  610. happens if the patient says some word other than FINE or LOUSY?
  611.   For example, what happens if the patient says TERRIBLE? Since 
  612. the program doesn't tell the computer how to react to TERRIBLE, 
  613. the computer won't print any reaction at all. The computer will 
  614. do just what it always does at the end of a program: it will say 
  615. OK (or READY or a similar word).
  616.                                          Let's enhance the 
  617. program, so that if the patient says TERRIBLE, the computer will 
  618. say TOUGH TURKEY. Add the shaded line:
  619. 10 INPUT "HOW ARE YOU";A$
  620. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD"
  621. 30 IF A$="LOUSY" THEN PRINT "TOO BAD"
  622. 40 IF A$="TERRIBLE" THEN PRINT "TOUGH TURKEY"
  623.                                          I feel the same way 
  624. Let's make the computer end the conversation by saying I FEEL THE 
  625. SAME WAY. Add the shaded line:
  626. 10 INPUT "HOW ARE YOU";A$
  627. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD"
  628. 30 IF A$="LOUSY" THEN PRINT "TOO BAD"
  629. 40 IF A$="TERRIBLE" THEN PRINT "TOUGH TURKEY"
  630. 50 PRINT "I FEEL THE SAME WAY"
  631. Since line 50 does not contain the word IF, the computer will 
  632. always print I FEEL THE SAME WAY at the end of the conversation, 
  633. regardless of what the patient says.
  634.                                          Here's a sample run:
  635. RUN
  636. HOW ARE YOU? FINE
  637. THAT'S GOOD
  638. I FEEL THE SAME WAY
  639.                                          Here's another:
  640. RUN
  641. HOW ARE YOU? TERRIBLE
  642. TOUGH TURKEY
  643. I FEEL THE SAME WAY
  644.                                          Another:
  645. RUN
  646. HOW ARE YOU? LONELY
  647. I FEEL THE SAME WAY
  648.                                          Another:
  649. RUN
  650. HOW ARE YOU? I DON'T WANT TO TALK WITH YOU
  651. I FEEL THE SAME WAY
  652.                                          Avoid monotony Having 
  653. the computer always say I FEEL THE SAME WAY is monotonous. To 
  654. make the program more interesting, let's make the computer say I 
  655. FEEL THE SAME WAY only if the patient doesn't say FINE, LOUSY, or 
  656. TERRIBLE. So if the patient says FINE, LOUSY, or TERRIBLE, let's 
  657. prevent the computer from saying I FEEL THE SAME WAY.
  658.                                          Whenever you want to 
  659. make the computer skip saying I FEEL THE SAME WAY, type the word 
  660. END, like this:
  661. 10 INPUT "HOW ARE YOU";A$
  662. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD": END
  663. 30 IF A$="LOUSY" THEN PRINT "TOO BAD": END
  664. 40 IF A$="TERRIBLE" THEN PRINT "TOUGH TURKEY": END
  665. 50 PRINT "I FEEL THE SAME WAY"
  666. In that program, if the patient types FINE, LOUSY, or TERRIBLE, 
  667. lines 20-40 make the computer print a two-word message and then 
  668. END, without printing I FEEL THE SAME WAY. The only way the 
  669. computer can reach line 50 (which prints I FEEL THE SAME WAY) is 
  670. if the patient avoids FINE, LOUSY, and TERRIBLE.
  671.                                          Here's a sample run:
  672. RUN
  673. HOW ARE YOU? FINE
  674. THAT'S GOOD
  675.                                          Here's another:
  676. RUN
  677. HOW ARE YOU? LONELY
  678. I FEEL THE SAME WAY
  679.  
  680.   Charge $50 After the computer's given the patient therapy, 
  681. let's make the computer charge $50. Just add this line:
  682. 60 PRINT "I HOPE YOU ENJOYED YOUR THERAPY--NOW YOU OWE $50"
  683.   To make sure the computer always goes to that line and collects 
  684. the $50, regardless of what the patient says, replace each END by 
  685. GO TO 60. Altogether, the program looks like this:
  686. 10 INPUT "HOW ARE YOU";A$
  687. 20 IF A$="FINE" THEN PRINT "THAT'S GOOD": GO TO 60
  688. 30 IF A$="LOUSY" THEN PRINT "TOO BAD": GO TO 60
  689. 40 IF A$="TERRIBLE" THEN PRINT "TOUGH TURKEY": GO TO 60
  690. 50 PRINT "I FEEL THE SAME WAY"
  691. 60 PRINT "I HOPE YOU ENJOYED YOUR THERAPY--NOW YOU OWE $50"
  692.   Here's a sample run:
  693. RUN
  694. HOW ARE YOU? FINE
  695. THAT'S GOOD
  696. I HOPE YOU ENJOYED YOUR THERAPY--NOW YOU OWE $50
  697.   Here's another:
  698. RUN
  699. HOW ARE YOU? LONELY
  700. I FEEL THE SAME WAY
  701. I HOPE YOU ENJOYED YOUR THERAPY--NOW YOU OWE $50
  702.   In that program, try changing the strings to make the computer 
  703. print smarter remarks, become a better therapist, and charge even 
  704. more money.
  705.  
  706.               Keywords
  707.   In that program, the only words the computer understands are 
  708. INPUT, IF, THEN, PRINT, GO, and TO. Those words are called 
  709. keywords.
  710.   Using just those keywords, you can write any program you wish! 
  711. Here's why. . . . 
  712.   Can a computer become President? To become President of the 
  713. United States, you need four basic skills.
  714.   First, you must be a good talker, so you can give effective 
  715. speeches saying ``Vote for me!'', express your views, and make 
  716. folks do what you want.
  717.   But even if you're a good talker, you're useless unless you're 
  718. also a good listener. You must be able to listen to people's 
  719. needs and ask, ``What can I do to make you happy and get you to 
  720. vote for me?''
  721.   But even if you're a good talker and listener, you're still 
  722. useless unless you can make decisions. Should you give more money 
  723. to poor people? Should you bomb the enemy? Which actions should 
  724. you take, and under what conditions?
  725.   But even if you're a good talker and listener and decision 
  726. maker, you still need one more trait to become President: you 
  727. must be able to take the daily grind of politics. You must, again 
  728. and again, shake hands, make compromises, and raise funds. You 
  729. must have the patience to put up with the repetitive monotony of 
  730. those chores.
  731.   So altogether, to become President you need to be a good talker 
  732. and listener and decision maker and also have the patience to put 
  733. up with monotonous repetition.
  734.                                          Those are exactly the 
  735. four qualities the computer has! The word PRINT turns the 
  736. computer into a good speech-maker: by using the word PRINT, you 
  737. can make the computer write whatever speech you wish. The word 
  738. INPUT turns the computer into a good listener: by using the word 
  739. INPUT, you can make the computer ask humans lots of questions, to 
  740. find out who the humans are and what they want. The words IF and 
  741. THEN turn the computer into a decision maker: the computer can 
  742. analyze the IF condition, determine whether that condition is 
  743. true, and act accordingly. Finally, the words GO and TO enable 
  744. the computer to perform loops, which the computer will repeat 
  745. patiently.
  746.                                          So by using the words 
  747. PRINT, INPUT, IF THEN, and GO TO, you can make the computer 
  748. imitate any intellectual human activity. Those four magic phrases 
  749. ___ PRINT, INPUT, IF THEN, and GO TO ___ are the only phrases you 
  750. need, to write whatever program you wish!
  751.                                          Yes, you can make the 
  752. computer imitate the President of the United States, do your 
  753. company's payroll, compose a beautiful poem, play a perfect game 
  754. of chess, contemplate the meaning of life, act as if it's falling 
  755. in love, or do whatever other intellectual or emotional task you 
  756. wish, by using just those four magic phrases. The only question 
  757. is: how? The Secret Guide to Computers teaches you how, by 
  758. showing you many examples of programs that do those remarkable 
  759. things.
  760.                                          What programmers believe 
  761. Yes, we programmers believe that all of life can be explained and 
  762. programmed. We believe all of life can be reduced to just those 
  763. four phrases: PRINT, INPUT, IF THEN, and GO TO. Programming is 
  764. the ultimate act of scientific reductionism: programmers reduce 
  765. all of life scientifically to just four phrases.
  766.                                          In addition to those 
  767. keywords (PRINT, INPUT, IF, THEN, GO, and TO), the computer 
  768. understands extra keywords also, such as END. Those extra 
  769. keywords aren't strictly necessary: if they hadn't been invented, 
  770. you could still write programs without them. But they make 
  771. programming easier.
  772.                                          A programmer is a person 
  773. who translates an ordinary English sentence (such as ``act like 
  774. the President'' or ``do the payroll'') into a series of BASIC 
  775. statements, using keywords such as PRINT, INPUT, IF THEN, GO TO, 
  776. and END.
  777.                                          The mysteries of life 
  778. Let's dig deeper into the mysteries of PRINT, INPUT, IF THEN, GO 
  779. TO, and the extra keywords. The deeper we dig, the more you'll 
  780. wonder: are you just a computer, made of flesh instead of wires? 
  781. Can everything that you do be explained in terms of PRINT, INPUT, 
  782. IF THEN, and GO TO?
  783.                                          By the time you finish 
  784. The Secret Guide to Computers, you'll know!
  785.    Mary Poppins meets Frankenstein
  786.   To make the computer interrogate a human, have the computer 
  787. ask:
  788. ARE YOU MALE OR FEMALE?
  789.   If the human answers MALE, let's make the computer say:
  790. SO IS FRANKENSTEIN
  791. If the human answers FEMALE, let's make the computer say:
  792. SO IS MARY POPPINS
  793.   Here's the program:
  794. 10 INPUT "ARE YOU MALE OR FEMALE";A$
  795. 20 IF A$="MALE" THEN PRINT "SO IS FRANKENSTEIN"
  796. 30 IF A$="FEMALE" THEN PRINT "SO IS MARY POPPINS"
  797.   Here's a sample run:
  798. RUN
  799. ARE YOU MALE OR FEMALE? MALE
  800. SO IS FRANKENSTEIN
  801.   Here's another:
  802. RUN
  803. ARE YOU MALE OR FEMALE? FEMALE
  804. SO IS MARY POPPINS
  805.   Neither MALE nor FEMALE? What does that program do if the human 
  806. says neither MALE nor FEMALE? What if the human says SUPER-MALE 
  807. or MACHO or NOT SURE or BOTH or YES? In those cases, the program 
  808. doesn't tell the computer how to reply, so the computer will make 
  809. no reply at all.
  810.   Let's improve the program, so that if the human says neither 
  811. MALE nor FEMALE the computer will reply ___ 
  812. PLEASE SAY MALE OR FEMALE
  813. ARE YOU MALE OR FEMALE?
  814. and force the human to answer the question correctly.
  815.   To do that, add this line ___ 
  816. 40 PRINT "PLEASE SAY MALE OR FEMALE": GO TO 10
  817. and put END at the end of lines 20 and 30, so the program looks 
  818. like this:
  819. 10 INPUT "ARE YOU MALE OR FEMALE";A$
  820. 20 IF A$="MALE" THEN PRINT "SO IS FRANKENSTEIN": END
  821. 30 IF A$="FEMALE" THEN PRINT "SO IS MARY POPPINS": END
  822. 40 PRINT "PLEASE SAY MALE OR FEMALE": GO TO 10
  823.   Line 10 makes the computer ask ``ARE YOU MALE OR FEMALE?'' and 
  824. wait for the human's answer, which is called A$. If the human's 
  825. answer is MALE, line 20 makes the computer print SO IS 
  826. FRANKENSTEIN and then end. If the human's answer is FEMALE, line 
  827. 30 makes the computer print SO IS MARY POPPINS and then END. If 
  828. the human's answer is neither MALE nor FEMALE, the computer skips 
  829. over lines 20 and 30, so it comes to line 40, which makes it 
  830. print PLEASE SAY MALE OR FEMALE and then go back to line 10, 
  831. which forces the human to answer the question again.
  832.   Here's a sample run:
  833. RUN
  834. ARE YOU MALE OR FEMALE? MALE
  835. SO IS FRANKENSTEIN
  836.   Here's another:
  837. RUN
  838. ARE YOU MALE OR FEMALE? FEMALE
  839. SO IS MARY POPPINS
  840.  
  841.                                          Another:
  842. RUN
  843. ARE YOU MALE OR FEMALE? MACHO
  844. PLEASE SAY MALE OR FEMALE
  845. ARE YOU MALE OR FEMALE? MALE
  846. SO IS FRANKENSTEIN
  847.                                          Another:
  848. RUN
  849. ARE YOU MALE OR FEMALE? SUPER-MALE
  850. PLEASE SAY MALE OR FEMALE
  851. ARE YOU MALE OR FEMALE? NONE OF YOUR BUSINESS
  852. PLEASE SAY MALE OR FEMALE
  853. ARE YOU MALE OR FEMALE? MAIL
  854. PLEASE SAY MALE OR FEMALE
  855. ARE YOU MALE OR FEMALE? MALE
  856. SO IS FRANKENSTEIN
  857.                                          In that program, if the 
  858. human makes a typing error and answers neither MALE nor FEMALE, 
  859. the computer arrives at line 40, which makes the computer gripe 
  860. and tell the human to try again. So the purpose of line 40 is to 
  861. react to errors. Line 40 is called an error-handling routine or 
  862. an error trap. That's because an error's like a vicious monster, 
  863. and line 40's purpose is to trap it.
  864.                                          Do you like Mary 
  865. Poppins? Let's extend the conversation. If the human says FEMALE, 
  866. let's make the computer say SO IS MARY POPPINS and then ask ``DO 
  867. YOU LIKE HER?'' If the human says YES, let's make the computer 
  868. say:
  869. I LIKE HER TOO--SHE IS MY MOTHER
  870. If the human says NO, let's make the computer say:
  871. NEITHER DO I--SHE STILL OWES ME A DIME
  872. If the human says neither YES nor NO, let's make the computer 
  873. say:
  874. PLEASE SAY YES OR NO
  875. DO YOU LIKE HER?
  876.                                          Here's the program:
  877. 10 INPUT "ARE YOU MALE OR FEMALE";A$
  878. 20 IF A$="MALE" THEN PRINT "SO IS FRANKENSTEIN": END
  879. 30 IF A$="FEMALE" THEN PRINT "SO IS MARY POPPINS": GO TO 100
  880. 40 PRINT "PLEASE SAY MALE OR FEMALE": GO TO 10
  881.  
  882. 100 INPUT "DO YOU LIKE HER";B$
  883. 110 IF B$="YES" THEN PRINT "I LIKE HER TOO--SHE IS MY MOTHER ": 
  884. END
  885. 120 IF B$="NO" THEN PRINT "NEITHER DO I--SHE STILL OWES ME A  
  886. DIME": END
  887. 130 PRINT "PLEASE SAY YES OR NO": GO TO 100
  888.                                          Line 30 says: if the 
  889. human's answer is FEMALE, print SO IS MARY POPPINS and then go to 
  890. line 100, which asks ``DO YOU LIKE HER?'' Lines 110 and 120 make 
  891. the computer react to the human's opinion of Mary Poppins. Line 
  892. 130 is like line 40: it's an error trap.
  893.                  Weird programs
  894.   The computer's abilities are limited only by your own 
  895. imagination ___ and your weirdness. Here are some weird programs 
  896. from weird minds. . . . 
  897.   Friends Like a human, the computer wants to meet new friends. 
  898. This program makes the computer show its true feelings:
  899. 10 INPUT "ARE YOU MY FRIEND";A$
  900. 20 IF A$="YES" THEN PRINT "THAT'S SWELL": END
  901. 30 IF A$="NO" THEN PRINT "GO JUMP IN A LAKE": END
  902. 40 PRINT "PLEASE SAY YES OR NO": GO TO 10
  903.   When you type RUN, the computer asks ``ARE YOU MY FRIEND?'' If 
  904. you say YES, the computer says THAT'S SWELL. If you say NO, the 
  905. computer says GO JUMP IN A LAKE.
  906.   Watch TV The most inventive programmers are kids. This program 
  907. was written by a girl in the sixth grade:
  908. 10 INPUT "CAN I COME OVER TO YOUR HOUSE TO WATCH T.V.";A$
  909. 20 IF A$="YES" THEN PRINT "THANKS.  I'LL BE THERE AT 5 P.M.": END
  910. 30 IF A$="NO" THEN PRINT "HUMPH!  YOUR FEET SMELL, ANYWAY.": END
  911. 40 PRINT "PLEASE SAY YES OR NO": GO TO 10
  912. When you type RUN, the computer asks to watch your TV. If you say 
  913. YES, the computer promises to come to your house at 5. If you 
  914. refuse, the computer insults your feet.
  915.   Honesty Another sixth-grade girl wrote this program, to test 
  916. your honesty:
  917. 10 PRINT "FKGJDFGKJ*#K$JSLF*/#$()$&(IKJNHBGD52:?./KSDJK$E(EF$#/JI
  918. K(*"
  919. 20 PRINT "FASDFJKL:JFRFVFJUNJI*&()JNE$#SKI#(!SERF HHW NNWAZ MAME 
  920. !!!"
  921. 30 PRINT "ZBB%%%%%##)))))FESDFJK DSFE N.D.JJUJASD EHWLKD******"
  922. 40 INPUT "DO YOU UNDERSTAND WHAT I SAID";A$
  923. 50 IF A$="NO" THEN PRINT "SORRY TO HAVE BOTHERED YOU": END
  924. 60 IF A$="YES" THEN GO TO 100
  925. 70 PRINT "PLEASE SAY YES OR NO": GO TO 10
  926.  
  927. 100 PRINT "SSFJSLFKDJFL++++4567345677839XSDWFEGF/#$&**()---==!!ZZ
  928. XX"
  929. 110 PRINT "###EDFHTG NVFDF MKJKF ==+--*$&% #RHFS SESD 
  930. DOPEKKKNJGFD DSBS"
  931. 120 INPUT "OKAY, WHAT DID I SAY";B$
  932. 130 PRINT "YOU ARE A LIAR, A LIAR, A BIG FAT LIAR!"
  933.   When you type RUN, lines 10-30 print nonsense. Then the 
  934. computer asks whether you understand that stuff. If you're honest 
  935. and answer NO, the computer will apologize. But if you pretend 
  936. that you understand the nonsense and answer YES, the computer 
  937. will print more nonsense, challenge you to translate it, wait for 
  938. you to fake a translation, and then scold you for lying.
  939.   Daddy's always right A Daddy wrote a program for his 
  940. five-year-old son, John.
  941.   When John runs the program and types his name, the computer 
  942. asks ``WHAT'S 2 AND 2?'' If John answers 4, the computer says NO, 
  943. 2 AND 2 IS 22. If he runs the program again and answers 22, the 
  944. computer says NO, 2 AND 2 IS 4. No matter how many times he runs 
  945. the program and how he answers the question, the computer says 
  946. he's wrong. But when Daddy runs the program, the computer 
  947. replies, YES, DADDY IS ALWAYS RIGHT.
  948.   Here's how Daddy programmed the computer:
  949. 10 INPUT "WHAT'S YOUR NAME";N$
  950. 20 INPUT "WHAT'S 2 AND 2";A
  951. 30 IF N$="DADDY" THEN PRINT "YES, DADDY IS ALWAYS RIGHT": END
  952. 40 IF A=4 THEN PRINT "NO, 2 AND 2 IS 22": END
  953. 50 PRINT "NO, 2 AND 2 IS 4"
  954.  
  955.  
  956.                  Fancy relations
  957.   You can make the IF clause very fancy:
  958. IF clauseMeaning
  959. IF A$="MALE"If A$ is ``MALE''
  960. IF A=4  If A is 4
  961. IF A<4  If A is less than 4
  962. IF A>4  If A is greater than 4
  963. IF A<=4 If A is less than or equal to 4
  964. IF A>=4 If A is greater than or equal to 4
  965. IF A<>4 If A is not 4
  966. IF A$<"MALE"If A is a word that comes before ``MALE'' in the 
  967. dictionary
  968. IF A$>"MALE"If A is a word that comes after ``MALE'' in the 
  969. dictionary
  970.   In the IF statement, the symbols =, <, >, <=, >=, and <> are 
  971. called relations.
  972.   When you write a relation, put the equal sign last:
  973. RightWrong
  974. <=  =<
  975. >=  =>
  976.   To say ``not equal to'' say ``less than or greater than'', like 
  977. this: <>.
  978.  
  979.                        OR
  980.   The computer understands the word OR. For example, here's how 
  981. to say, ``If X is either 7 or 8, print the word WONDERFUL'':
  982. IF X=7 OR X=8 THEN PRINT "WONDERFUL"
  983.   That example is composed of two conditions: the first condition 
  984. is ``X=7''; the second condition is ``X=8''. Those two conditions 
  985. combine, to form ``X=7 OR X=8'', which is called a compound 
  986. condition.
  987.   If you use the word OR, put it between two conditions.
  988. Right:IF X=7 OR X=8 THEN PRINT "WONDERFUL"(``X=7'' and ``X=8'' 
  989. are conditions.)
  990. Wrong:IF X=7 OR 8 THEN PRINT "WONDERFUL"(``8'' is not a 
  991. condition.)
  992.  
  993.                        AND
  994.   The computer understands the word AND. Here's how to say, ``If 
  995. P is more than 5 and less than 10, print TUNA FISH'':
  996. IF P>5 AND P<10 THEN PRINT "TUNA FISH"
  997. Here's how to say, ``If S is at least 60 and less than 65, print 
  998. YOU ALMOST FAILED'':
  999. IF S>=60 AND S<65 THEN PRINT "YOU ALMOST FAILED"
  1000. Here's how to say, ``If N is a number from 1 to 10, print THAT'S 
  1001. GOOD'':
  1002. IF N>=1 AND N<=10 THEN PRINT "THAT'S GOOD"
  1003.  
  1004.                       ELSE
  1005.   Here's how to say, ``If A is less than 18, print MINOR; but if 
  1006. A is not less than 18, print ADULT'':
  1007. IF A<18 THEN PRINT "MINOR" ELSE PRINT "ADULT"
  1008.   That line says to either PRINT ``MINOR'' or ELSE PRINT 
  1009. ``ADULT''. If A is less than 18, the computer will PRINT 
  1010. ``MINOR''; otherwise, the computer will PRINT ``ADULT''.
  1011.   Here's how to say, ``If A is less than 18, print MINOR and 
  1012. print YOUNG; if A is not less than 18, print ADULT and print 
  1013. OLD'':
  1014. IF A<18 THEN PRINT "MINOR": PRINT "YOUNG" ELSE PRINT "ADULT": 
  1015. PRINT "OLD"
  1016.   Primitive computers don't understand the word ELSE. To find out 
  1017. whether your computer is primitive, check the ``Versions of 
  1018. BASIC'' appendix.
  1019.  
  1020.  
  1021.                  DATA . . . READ
  1022.   Let's make the computer print this message:
  1023. I LOVE MEAT
  1024. I LOVE POTATOES
  1025. I LOVE LETTUCE
  1026. I LOVE TOMATOES
  1027. I LOVE BUTTER
  1028. I LOVE CHEESE
  1029. I LOVE ONIONS
  1030. I LOVE PEAS
  1031.   That message concerns this list of food: MEAT, POTATOES, 
  1032. LETTUCE, TOMATOES, BUTTER, CHEESE, ONIONS, PEAS. That list 
  1033. doesn't change: the computer continues to love those foods 
  1034. throughout the entire program.
  1035.   A list that doesn't change is called data. So in the message 
  1036. about food, the data is MEAT, POTATOES, LETTUCE, TOMATOES, 
  1037. BUTTER, CHEESE, ONIONS, PEAS.
  1038.   Whenever a problem involves data, put the data at the top of 
  1039. the program, like this:
  1040. 10 DATA MEAT,POTATOES,LETTUCE,TOMATOES,BUTTER,CHEESE,ONIONS,PEAS
  1041.   You must tell the computer to READ the DATA:
  1042. 20 READ A$
  1043. Line 20 makes the computer read the first datum (MEAT) and call 
  1044. it A$. So A$ is MEAT.
  1045.   Since A$ is MEAT, this line makes the computer print I LOVE 
  1046. MEAT:
  1047. 30 PRINT "I LOVE ";A$
  1048.   Hooray! We made the computer handle the first datum correctly: 
  1049. we made the computer print I LOVE MEAT. To make the computer 
  1050. handle the rest of the data (POTATOES, LETTUCE, etc.), tell the 
  1051. computer to READ the rest of the data: tell the computer to GO 
  1052. back to the READ statement. Since the READ statement is line 20, 
  1053. tell the computer to GO TO 20, like this:
  1054. 40 GO TO 20
  1055. Line 40 makes the computer GO back TO line 20, which reads the 
  1056. next datum (POTATOES).
  1057.   Altogether, the program looks like this:
  1058. 10 DATA MEAT,POTATOES,LETTUCE,TOMATOES,BUTTER,CHEESE,ONIONS,PEAS
  1059. 20 READ A$
  1060. 30 PRINT "I LOVE ";A$
  1061. 40 GO TO 20
  1062.   Lines 20-40 form a loop. Like most loops, the loop's bottom 
  1063. line says GO TO. Since the loop's top line says READ, the loop is 
  1064. called a READ loop. The computer goes round and round the loop.
  1065.   Each time the computer comes to line 20, it reads another 
  1066. datum. The first time it comes to line 20, it reads MEAT; the 
  1067. next time, it reads POTATOES; the next time, it reads LETTUCE; 
  1068. the next time, it reads TOMATOES; etc.
  1069.   Line 30 makes the computer PRINT what it's read.
  1070.   Altogether, the computer will print:
  1071. I LOVE MEAT
  1072. I LOVE POTATOES
  1073. I LOVE LETTUCE
  1074. I LOVE TOMATOES
  1075. I LOVE BUTTER
  1076. I LOVE CHEESE
  1077. I LOVE ONIONS
  1078. I LOVE PEAS
  1079. After the computer prints I LOVE PEAS, it comes to line 40 again, 
  1080. which makes it go back to line 20, so the computer tries to read 
  1081. even more data; but no more data remains! So the computer says:
  1082. OUT OF DATA
  1083. Then the computer stops.
  1084.                                                      Most 
  1085. practical computer programs involve data (a list that doesn't 
  1086. change). As a programmer, your job is to notice what the data is 
  1087. and begin your program by saying DATA. After you type the data, 
  1088. write the program's next line, which should say READ. Farther 
  1089. down in your program, you should say GO TO so that you create 
  1090. READ loop. Your program consists of two parts: the DATA and the 
  1091. READ loop.
  1092.                                                      In the DATA 
  1093. statement, you must put quotation marks around any string that 
  1094. contains a comma or colon. For example, if one of the foods is 
  1095. ``HOT, JUICY, BIG, THICK, STEAKS'' (which contains commas), you 
  1096. must put quotation marks around it.
  1097.  
  1098.                                                      Avoiding OUT OF DATA
  1099.                                                      When you run 
  1100. that sample program about food, the last three lines the computer 
  1101. prints are:
  1102. I LOVE ONIONS
  1103. I LOVE PEAS
  1104. OUT OF DATA
  1105.                                                      Instead of 
  1106. saying OUT OF DATA, let's make the computer say ``I LOVE ALL 
  1107. THOSE FOODS'', so that the last three lines look like this:
  1108. I LOVE ONIONS
  1109. I LOVE PEAS
  1110. I LOVE ALL THOSE FOODS
  1111. Here's how. . . . 
  1112.                                                      Underneath 
  1113. the data, say DATA END:
  1114. 15 DATA END
  1115. When the computer reads the DATA END, make the computer say I 
  1116. LOVE ALL THOSE FOODS and end:
  1117. 20 READ A$: IF A$="END" THEN PRINT "I LOVE ALL THOSE FOODS": END
  1118.   The DATA END underneath the data is called the end mark, 
  1119. because it marks the data's end. The routine that says ___ 
  1120.             IF A$="END" THEN PRINT "I LOVE ALL THOSE FOODS": END
  1121. is called the end routine, because the computer does that routine 
  1122. at the end.
  1123.  
  1124.                      RESTORE
  1125.   That program prints one copy of the computer's favorite foods. 
  1126. If you want the computer to print many copies, change line 20 to 
  1127. this:
  1128. 20 READ A$: IF A$="END" THEN PRINT "I LOVE ALL THOSE FOODS": 
  1129. RESTORE: GO TO 20
  1130. The word RESTORE tells the computer to go back to the beginning 
  1131. of the data. The computer will print:
  1132. I LOVE MEAT
  1133. I LOVE POTATOES
  1134. I LOVE LETTUCE
  1135. I LOVE TOMATOES
  1136. I LOVE BUTTER
  1137. I LOVE CHEESE
  1138. I LOVE ONIONS
  1139. I LOVE PEAS
  1140. I LOVE ALL THOSE FOODS
  1141. I LOVE MEAT
  1142. I LOVE POTATOES
  1143. I LOVE LETTUCE
  1144. I LOVE TOMATOES
  1145. I LOVE BUTTER
  1146. I LOVE CHEESE
  1147. I LOVE ONIONS
  1148. I LOVE PEAS
  1149. I LOVE ALL THOSE FOODS
  1150. I LOVE MEAT
  1151. I LOVE POTATOES
  1152. etc.
  1153. The computer will print copies of what it likes again and again, 
  1154. forever, unless you abort the program.
  1155.  
  1156.                 Henry the Eighth
  1157.   Let's make the computer print this nursery rhyme:
  1158. I love ice cream
  1159. I love red
  1160. I love ocean
  1161. I love bed
  1162. I love tall grass
  1163. I love to wed
  1164.  
  1165. I love candles
  1166. I love divorce
  1167. I love my kingdom
  1168. I love my horse
  1169. I love you
  1170. Of course, of course,
  1171. For I am Henry the Eighth!
  1172.   If you own a jump rope, have fun: try to recite that poem while 
  1173. skipping rope!
  1174.   This program makes the computer recite the poem repeatedly:
  1175. 10 DATA ICE CREAM,RED,OCEAN,BED,TALL GRASS,TO WED
  1176. 11 DATA CANDLES,DIVORCE,MY KINGDOM,MY HORSE,YOU
  1177. 15 DATA END
  1178. 20 READ A$: IF A$="END" THEN PRINT "OF COURSE, OF COURSE,": PRINT 
  1179. "FOR I AM HENR
  1180. Y THE EIGHTH!": PRINT: RESTORE: GO TO 20
  1181. 30 PRINT "I LOVE ";A$
  1182. 35 IF A$="TO WED" THEN PRINT
  1183. 40 GO TO 20
  1184.   Since the data's too long to fit on a single line, I've put 
  1185. part of the data in line 10 and the rest in line 11. Each line of 
  1186. data must begin with the word DATA. In each line, put commas 
  1187. between the items. Do not put a comma at the end of the line.
  1188.   The program resembles the previous one. The only new line is 
  1189. 35, which makes the computer leave a blank line underneath ``TO 
  1190. WED'', to mark the bottom of the first verse.
  1191.  
  1192.                       Party
  1193.   Let's throw a party! To make the party yummy, let's ask each 
  1194. guest to bring a kind of food that resembles the guest's name. 
  1195. For example, let's have Sal bring salad, Russ bring Russian 
  1196. dressing, Sue bring soup, Tom bring turkey, Winnie bring wine, 
  1197. Kay bring cake, and Al bring Alka-Seltzer.
  1198.   Let's send all those people invitations, in this form:
  1199. DEAR _____________
  1200.      person's name
  1201.  
  1202.      WE'RE THROWING A PARTY AT THE SECRET CLUBHOUSE TOMORROW AT 
  1203. NOON!
  1204.  
  1205. PLEASE BRING ____
  1206.              food
  1207.   To make the computer print all the invitations, begin by 
  1208. feeding the computer the DATA:
  1209. 10 DATA SAL,SALAD,RUSS,RUSSIAN DRESSING,SUE,SOUP,TOM,TURKEY
  1210. 20 DATA WINNIE,WINE,KAY,CAKE,AL,ALKA-SELTZER
  1211.   The data comes in pairs; the first pair consists of SAL and 
  1212. SALAD. Tell the computer to READ each pair of DATA:
  1213. 30 READ P$,F$
  1214. Line 30 makes the computer read the first pair of data; so P$ is 
  1215. the first person (SAL), and F$ is his food (SALAD).
  1216.   These lines print the letter:
  1217. 40 PRINT "DEAR ";P$
  1218. 50 PRINT "     WE'RE THROWING A PARTY AT THE SECRET CLUBHOUSE 
  1219. TOMORROW AT NOON!"
  1220. 60 PRINT "PLEASE BRING ";F$
  1221. At the end of the letter, leave two blank lines, to make room for 
  1222. your signature:
  1223. 70 PRINT
  1224. 80 PRINT
  1225.   Then complete the READ loop, by making the computer go back to 
  1226. the beginning of the loop, to read the next pair:
  1227. 90 GO TO 30
  1228.   The computer will print a letter to each person, like this:
  1229. DEAR SAL
  1230.      WE'RE THROWING A PARTY AT THE SECRET CLUBHOUSE TOMORROW AT 
  1231. NOON!
  1232. PLEASE BRING SALAD
  1233.  
  1234.  
  1235. DEAR RUSS
  1236.      WE'RE THROWING A PARTY AT THE SECRET CLUBHOUSE TOMORROW AT 
  1237. NOON!
  1238. PLEASE BRING RUSSIAN DRESSING
  1239.  
  1240.  
  1241. etc.
  1242.   After printing all the letters, the computer will say:
  1243. OUT OF DATA
  1244.   Instead of saying OUT OF DATA, let's make the computer say:
  1245. I'VE FINISHED WRITING THE LETTERS
  1246. To do that, put END at the end of the data, twice ___ 
  1247. 25 DATA END,END
  1248. and say what to do when the computer reaches the END:
  1249. 30 READ P$,F$: IF P$="END" THEN PRINT "I'VE FINISHED WRITING THE 
  1250. LETTERS": END
  1251. You need two ENDs at the end of the data, because the READ 
  1252. statement says to read two strings (P$ and F$).
  1253.  
  1254.                 Debts
  1255.   Suppose these people owe you things:
  1256. PersonWhat the person owes
  1257. Bob   $537.29
  1258. Mike  a dime
  1259. Sue   2 golf balls
  1260. Harry a steak dinner at Mario's
  1261. Mommy a kiss
  1262.   Let's remind those people of their debt, by writing them 
  1263. letters, in this form:
  1264. DEAR _____________
  1265.      person's name
  1266.  
  1267.      I JUST WANT TO REMIND YOU...
  1268.  
  1269. THAT YOU STILL OWE ME ____
  1270.                       debt
  1271. Begin with the DATA:
  1272. 10 DATA BOB,$537.29,MIKE,A DIME,SUE,2 GOLF BALLS
  1273. 20 DATA HARRY,A STEAK DINNER AT MARIO'S,MOMMY,A KISS
  1274.   The data comes in pairs; the first pair consists of BOB and 
  1275. $537.29. Tell the computer to READ each pair of DATA:
  1276. 30 READ P$,D$
  1277. Line 30 makes the computer read the first pair of data; so P$ is 
  1278. the first person (BOB), and D$ is his debt ($537.29).
  1279.   Here's the rest of the program:
  1280. 40 PRINT "DEAR ";P$
  1281. 50 PRINT "     I JUST WANT TO REMIND YOU..."
  1282. 50 PRINT "THAT YOU STILL OWE ME ";D$
  1283. 70 PRINT
  1284. 80 PRINT
  1285. 90 GO TO 30
  1286.   The computer will print a letter to each person, like this:
  1287. DEAR BOB
  1288.      I JUST WANT TO REMIND YOU...
  1289. THAT YOU STILL OWE ME $537.29
  1290.  
  1291.  
  1292. DEAR MIKE
  1293.      I JUST WANT TO REMIND YOU...
  1294. THAT YOU STILL OWE ME A DIME
  1295.  
  1296.  
  1297. etc.
  1298.   After printing all the letters, the computer will say:
  1299. OUT OF DATA
  1300.   To prevent the computer from saying OUT OF DATA, add line 25 
  1301. and retype line 30, as follows:
  1302. 25 DATA END,END
  1303. 30 READ P$,D$: IF P$="END" THEN PRINT "I'VE FINISHED WRITING  THE 
  1304. LETTERS": END
  1305.  
  1306.                                                        Diets
  1307.                                          Suppose you're running a 
  1308. diet clinic and get these results:
  1309. Person                                       Weight beforeWeight 
  1310. after
  1311. Joe                                          273 pounds219 pounds
  1312. Mary                                         412 pounds371 pounds
  1313. Bill                                         241 pounds173 pounds
  1314. Sam                                          309 pounds198 pounds
  1315. Here's how to make the computer print a nice report. . . . 
  1316.                                          Begin by feeding it the 
  1317. DATA:
  1318. 10 DATA JOE,273,219,MARY,412,371,BILL,241,173,SAM,309,198
  1319.                                          The DATA comes in 
  1320. triplets: the first triplet consists of JOE, 273, and 219. Tell 
  1321. the computer to READ each triplet of DATA:
  1322. 20 READ N$,B,A
  1323. That line makes the computer read the first triplet of data; so 
  1324. N$ is the first person's name (JOE), B is his weight before 
  1325. (273), and A is his weight after (219). Since B and A stand for 
  1326. numbers instead of strings, they don't have any dollar signs.
  1327.                                          These lines print the 
  1328. report about him:
  1329. 30 PRINT N$;" WEIGHED";B;"POUNDS BEFORE ATTENDING THE DIET C
  1330. LINIC"
  1331. 40 PRINT "BUT WEIGHED JUST";A;"POUNDS AFTERWARDS"
  1332. 50 PRINT "THAT'S A LOSS OF";B-A;"POUNDS"
  1333. At the end of that report about him, leave a blank line:
  1334. 60 PRINT
  1335.                                          Then complete the READ 
  1336. loop, by making the computer go back to the loop's beginning:
  1337. 70 GO TO 20
  1338.                                          The computer will print:
  1339. JOE WEIGHED 273 POUNDS BEFORE ATTENDING THE DIET CLINIC
  1340. BUT WEIGHED JUST 219 POUNDS AFTERWARDS
  1341. THAT'S A LOSS OF 54 POUNDS
  1342.  
  1343. MARY WEIGHED 412 POUNDS BEFORE ATTENDING THE DIET CLINIC
  1344. BUT WEIGHED JUST 371 POUNDS AFTERWARDS
  1345. THAT'S A LOSS OF 41 POUNDS
  1346.  
  1347. etc.
  1348.                                          At the end the computer 
  1349. will say OUT OF DATA. Instead, let's make it say:
  1350. COME TO OUR DIET CLINIC!
  1351. To do that, put END and two zeros at the end of the data ___ 
  1352. 15 DATA END,0,0
  1353. and say what to do when the computer reaches the END:
  1354. 20 READ N$,B,A: IF N$="END" THEN PRINT "COME TO THE DIET CLI 
  1355. NIC!": END
  1356. You need the two zeros after the END, because the READ statement 
  1357. says to read two numbers (B and A) after the string N$. If you 
  1358. omit the zeros, the computer will say OUT OF DATA. If you hate 
  1359. zeros, you can use other numbers instead; but most programmers 
  1360. prefer zeros.
  1361.       French colors
  1362.   Let's make the computer translate colors into French. For 
  1363. example, if the human says RED, we'll make the computer say the 
  1364. French equivalent, which is:
  1365. ROUGE
  1366.   Altogether, a run will look like this:
  1367. RUN
  1368. WHICH COLOR INTERESTS YOU? RED
  1369. IN FRENCH, IT'S ROUGE
  1370.   The program begins simply:
  1371. 10 INPUT "WHICH COLOR INTERESTS YOU";C$
  1372.   Next, we must make the computer translate the color into 
  1373. French. To do so, feed the computer this English-French 
  1374. dictionary:
  1375. EnglishFrench
  1376. white blanc
  1377. yellowjaune
  1378. orangeorange
  1379. red   rouge
  1380. green vert
  1381. blue  bleu
  1382. brown brun
  1383. black noir
  1384.  
  1385. That dictionary becomes the data:
  1386. 20 DATA WHITE,BLANC,YELLOW,JAUNE,ORANGE,ORANGE,RED,ROUGE
  1387. 30 DATA GREEN,VERT,BLUE,BLEU,BROWN,BRUN,BLACK,NOIR
  1388.                              The data comes in pairs; the first 
  1389. pair consists of WHITE and BLANC. Tell the computer to READ each 
  1390. pair of DATA:
  1391. 40 READ E$,F$
  1392. That line makes the computer read the first pair of data; so E$ 
  1393. is the first English color (WHITE), and F$ is its French 
  1394. equivalent (BLANC).
  1395.                              But that pair of data might be the 
  1396. wrong pair. For example, if the human requested RED, the human 
  1397. does not want the pair of WHITE and BLANC; instead, the human 
  1398. wants the pair of RED and ROUGE. Tell the computer that if the 
  1399. human's input (RED) doesn't match the English in the pair, go 
  1400. read another pair:
  1401. 50 IF E$<>C$ THEN GO TO 40
  1402. That line says: if E$ (which is the English in the data) is not 
  1403. C$ (the color the human requested), go to 40 (which reads another 
  1404. pair).
  1405.                              Lines 40 and 50 form a loop. The 
  1406. computer goes round and round the loop, until it finds the pair 
  1407. of data that matches the human's request. Since the loop's 
  1408. purpose is to search for matching data, it's called a search 
  1409. loop.
  1410.                              After the computer's found the 
  1411. correct English-French pair, make the computer print the French:
  1412. 60 PRINT "IN FRENCH, IT'S ";F$
  1413.                              Altogether, the program looks like 
  1414. this. . . . 
  1415. Ask the human:                         10 INPUT "WHICH COLOR 
  1416. INTERESTS YOU";C$
  1417. Use this dictionary:                   20 DATA 
  1418. WHITE,BLANC,YELLOW,JAUNE,ORANGE,ORANGE,RED,ROUGE
  1419.                                        30 DATA 
  1420. GREEN,VERT,BLUE,BLEU,BROWN,BRUN,BLACK,NOIR
  1421. Look at the dictionary:                40 READ E$,F$
  1422. If not found, try again:               50 IF E$<>C$ THEN GO TO 40
  1423. Print the French:                      60 PRINT "IN FRENCH, IT'S 
  1424. ";F$
  1425.                              Here's a sample run:
  1426. RUN
  1427. WHICH COLOR INTERESTS YOU? RED
  1428. IN FRENCH, IT'S ROUGE
  1429.                              Here's another:
  1430. RUN
  1431. WHICH COLOR INTERESTS YOU? BROWN
  1432. IN FRENCH, IT'S BRUN
  1433.                              Here's another:
  1434. RUN
  1435. WHICH COLOR INTERESTS YOU? PINK
  1436. OUT OF DATA
  1437. The computer says OUT OF DATA because it can't find PINK in the 
  1438. data.
  1439.                              Instead of saying OUT OF DATA, let's 
  1440. make the computer say I WASN'T TAUGHT THAT COLOR. To do that, put 
  1441. END at the end of the data; and when the computer reaches the 
  1442. END, make the computer say I WASN'T TAUGHT THAT COLOR:
  1443. 10 INPUT "WHICH COLOR INTERESTS YOU";C$
  1444. 20 DATA WHITE,BLANC,YELLOW,JAUNE,ORANGE,ORANGE,RED,ROUGE
  1445. 30 DATA GREEN,VERT,BLUE,BLEU,BROWN,BRUN,BLACK,NOIR
  1446. 35 DATA END,END
  1447. 40 READ E$,F$: IF E$="END" THEN PRINT "I WASN'T TAUGHT THAT 
  1448. COLOR": END
  1449. 50 IF E$<>C$ THEN GO TO 40
  1450. 60 PRINT "IN FRENCH, IT'S ";F$
  1451.                              After line 60, the program just 
  1452. ends. Instead of letting the computer end, let's make it 
  1453. automatically rerun the program and translate another color. To 
  1454. do that, say GO TO and RESTORE:
  1455. 10 INPUT "WHICH COLOR INTERESTS YOU";C$
  1456. 20 DATA WHITE,BLANC,YELLOW,JAUNE,ORANGE,ORANGE,RED,ROUGE
  1457. 30 DATA GREEN,VERT,BLUE,BLEU,BROWN,BRUN,BLACK,NOIR,END,END
  1458. 35 DATA END,END
  1459. 40 READ E$,F$: IF E$="END" THEN PRINT "I WASN'T TAUGHT THAT 
  1460. COLOR": GO TO 70
  1461. 50 IF E$<>C$ THEN GO TO 40
  1462. 60 PRINT "IN FRENCH, IT'S ";F$
  1463. 70 RESTORE 
  1464. 80 GO TO 10
  1465.  
  1466.            FOR . . . NEXT
  1467.   Let's make the computer print every number from 1 to 100, like 
  1468. this:
  1469.  1
  1470.  2
  1471.  3
  1472.  4
  1473.  5
  1474.  6
  1475.  7
  1476.  etc.
  1477.  100
  1478.   To do that, type this line ___ 
  1479. 20     PRINT X
  1480. and also say that you want X to be every number from 1 to 100, 
  1481. like this:
  1482. 10 FOR X = 1 TO 100
  1483. 20     PRINT X
  1484.   Whenever you write a program that contains the word FOR, you 
  1485. must say NEXT. So your program should look like this:
  1486. 10 FOR X = 1 TO 100
  1487. 20     PRINT X
  1488. 30 NEXT X
  1489. That program works; it makes the computer print every number from 
  1490. 1 to 100.
  1491.   Here's how it works. . . . 
  1492.   The computer begins at line 10, which says that you want X to 
  1493. be every number from 1 to 100. So X starts at 1.
  1494.   Then the computer comes to line 20, which says to print X; so 
  1495. the computer prints:
  1496.  1
  1497.   Then the computer comes to line 30, which says to do the same 
  1498. thing for the next X, and for the next X, and for the next X; so 
  1499. the computer prints 2, and 3, and 4, and so on, all the way up to 
  1500. 100.
  1501.   The computer prints many numbers, because the computer does 
  1502. line 20 many times (once for each X).
  1503.   The computer does line 20 many times, because line 20 is 
  1504. between the words FOR and NEXT: it's underneath FOR, and above 
  1505. NEXT. The computer repeats anything that's between the words FOR 
  1506. and NEXT.
  1507.   Most programmers get in the habit of indenting everything that 
  1508. comes between the words FOR and NEXT; so in that program, I 
  1509. indented the statement that says PRINT X. To make the 
  1510. indentation, you can hit the SPACE bar repeatedly.
  1511.   In line 30, if you're too lazy to type the X, you can omit it 
  1512. and just type:
  1513. 30 NEXT
  1514.  
  1515.                                                 When men meet women
  1516.                                          Let's make the computer 
  1517. print these lyrics:
  1518. I SAW 2 MEN
  1519. MEET 2 WOMEN
  1520. TRA-LA-LA!
  1521.  
  1522. I SAW 3 MEN
  1523. MEET 3 WOMEN
  1524. TRA-LA-LA!
  1525.  
  1526. I SAW 4 MEN
  1527. MEET 4 WOMEN
  1528. TRA-LA-LA!
  1529.  
  1530. I SAW 5 MEN
  1531. MEET 5 WOMEN
  1532. TRA-LA-LA!
  1533.  
  1534. THEY ALL HAD A PARTY!
  1535. HA-HA-HA!
  1536.                                          To do that, type these 
  1537. lines ___ 
  1538. The first line of each verse:  20     PRINT "I SAW";X;"MEN"
  1539. The second line of each verse: 30     PRINT "MEET";X;"WOMEN"
  1540. The third line of each verse:  40     PRINT "TRA-LA-LA!"
  1541. Blank line under each verse:   50     PRINT
  1542. and make X be every number from 2 up to 5:
  1543. 10 FOR X = 2 TO 5
  1544. 20     PRINT "I SAW";X;"MEN"
  1545. 30     PRINT "MEET";X;"WOMEN"
  1546. 40     PRINT "TRA-LA-LA!"
  1547. 50     PRINT
  1548. 60 NEXT
  1549.                                          At the end of the song, 
  1550. print the closing couplet:
  1551. 10 FOR X = 2 TO 5
  1552. 20     PRINT "I SAW";X;"MEN"
  1553. 30     PRINT "MEET";X;"WOMEN"
  1554. 40     PRINT "TRA-LA-LA!"
  1555. 50     PRINT
  1556. 60 NEXT
  1557. 70 PRINT "THEY ALL HAD A PARTY!"
  1558. 80 PRINT "HA-HA-HA!"            
  1559. That program makes the computer print the entire song.
  1560.                                          Here's an analysis:
  1561.                                                        10 FOR X = 
  1562. 2 TO 5
  1563. The computer will do the                               20     
  1564. PRINT "I SAW";X;"MEN" 
  1565. indented lines repeatedly,                             30     
  1566. PRINT "MEET";X;"WOMEN"
  1567. for X=2, X=3, X=4, and X=5.                            40     
  1568. PRINT "TRA-LA-LA!"    
  1569.                                                        50     
  1570. PRINT                 
  1571.                                                        60 NEXT
  1572. Then the computer will                                 70 PRINT 
  1573. "THEY ALL HAD A PARTY!"
  1574. print this couplet once.                               80 PRINT 
  1575. "HA-HA-HA!"            
  1576.                                          Since the computer does 
  1577. lines 20-50 repeatedly, those lines form a loop. Here's the 
  1578. general rule: the statements between FOR and NEXT form a loop. 
  1579. The computer goes round and round the loop, for X=2, X=3, X=4, 
  1580. and X=5. Altogether, it goes around the loop 4 times, which is a 
  1581. finite number. Therefore, the loop is finite.
  1582.                                          If you don't like the 
  1583. letter X, choose a different letter. For example, you can choose 
  1584. the letter I:
  1585. 10 FOR I = 2 TO 5
  1586. 20     PRINT "I SAW";I;"MEN"
  1587. 30     PRINT "MEET";I;"WOMEN"
  1588. 40     PRINT "TRA-LA-LA!"
  1589. 50     PRINT
  1590. 60 NEXT
  1591. 70 PRINT "THEY ALL HAD A PARTY!"
  1592. 80 PRINT "HA-HA-HA!"
  1593.  
  1594.   When using the word FOR, most programmers prefer the letter I; 
  1595. most programmers say ``FOR I'' instead of ``FOR X''. Saying ``FOR 
  1596. I'' is an ``old tradition''. Following that tradition, the rest 
  1597. of this book says ``FOR I'' (instead of ``FOR X''), except in 
  1598. situations where some other letter feels more natural.
  1599.  
  1600.                Squares
  1601.   To find the square of a number, multiply the number by itself. 
  1602. The square of 3 is ``3 times 3'', which is 9. The square of 4 is 
  1603. ``4 times 4'', which is 16.
  1604.   Let's make the computer print the square of 3, 4, 5, etc., up 
  1605. to 100, like this:
  1606. THE SQUARE OF 3 IS 9
  1607. THE SQUARE OF 4 IS 16
  1608. THE SQUARE OF 5 IS 25
  1609. THE SQUARE OF 6 IS 36
  1610. THE SQUARE OF 7 IS 49
  1611. etc.
  1612. THE SQUARE OF 100 IS 10000
  1613.   To do that, type this line ___ 
  1614. 20     PRINT "THE SQUARE OF";I;"IS";I*I
  1615. and make I be every number from 3 up to 100, like this:
  1616. 10 FOR I = 3 TO 100
  1617. 20     PRINT "THE SQUARE OF";I;"IS";I*I
  1618. 30 NEXT
  1619.  
  1620.            Secret meeting
  1621.   This program prints 12 copies of the same message:
  1622. 10 FOR I = 1 TO 12
  1623. 20     PRINT "HUSH, HUSH!"
  1624. 30     PRINT "  WE'RE HAVING A SECRET MEETING..."
  1625. 40     PRINT "    IN THE COMPUTER ROOM..."
  1626. 50     PRINT "      TONIGHT..."
  1627. 60     PRINT "        AT 2 A.M."
  1628. 70     PRINT "          WEAR A FUNNY HAT."
  1629. 80     PRINT
  1630. 90     PRINT
  1631. 100 NEXT
  1632.   Lines 80 and 90 leave blank lines at the end of each copy for 
  1633. your signature.
  1634.  
  1635.               Midnight
  1636.   This program makes the computer count to midnight:
  1637. 10 FOR I = 1 TO 11
  1638. 20     PRINT I
  1639. 30 NEXT
  1640. 40 PRINT "MIDNIGHT"
  1641.   The computer will print:
  1642.  1
  1643.  2
  1644.  3
  1645.  4
  1646.  5
  1647.  6
  1648.  7
  1649.  8
  1650.  9
  1651.  10
  1652.  11
  1653. MIDNIGHT
  1654.  
  1655.                                          Let's put a semicolon at 
  1656. the end of line 20:
  1657. 10 FOR I = 1 TO 11
  1658. 20     PRINT I;
  1659. 30 NEXT
  1660. 40 PRINT "MIDNIGHT"
  1661. The semicolon makes the computer print each item on the same 
  1662. line, like this:
  1663.  1  2  3  4  5  6  7  8  9  10  11 MIDNIGHT
  1664.                                          If you want the computer 
  1665. to press the ENTER key before MIDNIGHT, insert a PRINT line:
  1666. 10 FOR I = 1 TO 11
  1667. 20     PRINT I;
  1668. 30 NEXT
  1669. 35 PRINT
  1670. 40 PRINT "MIDNIGHT"
  1671. Line 35 makes the computer press the ENTER key just before 
  1672. MIDNIGHT, so the computer will print MIDNIGHT on a separate line, 
  1673. like this:
  1674.  1  2  3  4  5  6  7  8  9  10  11
  1675. MIDNIGHT
  1676.                                          In line 20, the 
  1677. semicolon means: do not press the ENTER key after I. Line 35 
  1678. means: do press the ENTER key. So line 35 undoes line 20 and 
  1679. makes the computer press the ENTER key before MIDNIGHT.
  1680.                                          Let's make the computer 
  1681. count to midnight 3 times, like this:
  1682.  1  2  3  4  5  6  7  8  9  10  11
  1683. MIDNIGHT
  1684.  1  2  3  4  5  6  7  8  9  10  11
  1685. MIDNIGHT
  1686.  1  2  3  4  5  6  7  8  9  10  11
  1687. MIDNIGHT
  1688. To do that, put the entire program between the words FOR and 
  1689. NEXT:
  1690. 5 FOR A = 1 TO 3
  1691. 10     FOR I = 1 TO 11
  1692. 20         PRINT I;
  1693. 30     NEXT
  1694. 35     PRINT
  1695. 40     PRINT "MIDNIGHT"
  1696. 50 NEXT
  1697. That version contains a loop inside a loop: the loop that says 
  1698. ``FOR I'' is inside the loop that says ``FOR A''. The A loop is 
  1699. called the outer loop; the I loop is called the inner loop. The 
  1700. inner loop's variable must differ from the outer loop's. Since we 
  1701. called the inner loop's variable ``I'', the outer loop's variable 
  1702. must not be called ``I''; so I picked the letter A instead.
  1703.                                          Programmers often think 
  1704. of the outer loop as a bird's nest, and the inner loop as an egg 
  1705. inside the nest. So programmers say the inner loop is nested in 
  1706. the outer loop; the inner loop is a nested loop.
  1707.                  Favorite color
  1708.   This program plays a guessing game:
  1709. 10 PRINT "I'LL GIVE YOU FIVE GUESSES...."
  1710. 20 FOR I = 1 TO 5
  1711. 30     INPUT "WHAT'S MY FAVORITE COLOR";G$
  1712. 40     IF G$="PINK" THEN GO TO 100
  1713. 50     PRINT "NO."
  1714. 60 NEXT
  1715. 70 PRINT "SORRY, YOUR FIVE GUESSES ARE UP! YOU LOSE."
  1716. 80 END
  1717.  
  1718. 100 PRINT "CONGRATULATIONS! YOU DISCOVERED MY FAVORITE COLOR."
  1719. 110 PRINT "IT TOOK YOU";I;"GUESSES".
  1720.   Line 10 warns the human that only five guesses are allowed. 
  1721. Line 20 makes the computer count from 1 to 5; to begin, I is 1. 
  1722. Line 30 asks the human to guess the computer's favorite color; 
  1723. the guess is called G$.
  1724.   If the guess is PINK, the computer jumps from line 40 to line 
  1725. 100, prints CONGRATULATIONS, and tells how many guesses the human 
  1726. took. But if the guess is not PINK, the computer proceeds from 
  1727. line 40 to line 50, prints NO, and goes on to the next guess.
  1728.   If the human guesses five times without success, the computer 
  1729. proceeds from line 60 to line 70 and prints SORRY . . . YOU LOSE.
  1730.   For example, if the human's third guess is PINK, the computer 
  1731. prints:
  1732. CONGRATULATIONS! YOU DISCOVERED MY FAVORITE COLOR.
  1733. IT TOOK YOU 3 GUESSES.
  1734.   If the human's very first guess is PINK, the computer prints:
  1735. CONGRATULATIONS! YOU DISCOVERED MY FAVORITE COLOR.
  1736. IT TOOK YOU 1 GUESSES.
  1737. Saying ``1 GUESSES'' is bad grammar but understandable.
  1738.   Lines 20-60 form a loop. Line 20 says the loop will normally be 
  1739. done five times. The line after the loop, line 70, is the loop's 
  1740. normal exit. But if the human happens to input PINK, the computer 
  1741. jumps out of the loop early, to line 100, which is the loop's 
  1742. abnormal exit.
  1743.  
  1744.                   Finite pause
  1745.   Have you ever met someone who acts romantic, then suddenly says 
  1746. something cruel that breaks the romance?
  1747.   Let's make the computer act that way. For example, let's make 
  1748. the computer begin by crooning a romantic message:
  1749. 10 PRINT "YOUR LUSCIOUS LIPS SMELL LIKE THE FINEST WINE"
  1750.   Then let's make the computer pause, to give the human a chance 
  1751. to admire that romantic message and make the human wait in 
  1752. suspense for the next outburst of computer emotion. Here's how to 
  1753. make the computer pause:
  1754. 20 FOR I = 1 TO 7000: NEXT
  1755. That line makes the computer pause, while it counts up to 7000. 
  1756. That line makes the computer mumble to itself, ``I is 1, I is 2, 
  1757. I is 3, I is 4, . . . '' without printing anything on the screen. 
  1758. While the computer is secretly and silently mumbling to itself up 
  1759. to 7000, the human has a chance to read and admire the romantic 
  1760. message printed by line 10.
  1761.   Finally, let's make the computer analyze the meaning of lips 
  1762. smelling like wine:
  1763. 30 PRINT "YOU MUST BE DRUNK"
  1764.   So altogether, that program makes the computer print YOUR 
  1765. LUSCIOUS LIPS SMELL LIKE THE FINEST WINE, then pause, then say 
  1766. YOU MUST BE DRUNK.
  1767.   In that program, line 20 makes the computer pause, by making 
  1768. the computer silently mumble up to 7000. The typical computer 
  1769. mumbles 1000 times per second. Since line 20 says to mumble 7000 
  1770. times, that line takes 7 seconds altogether, and so it makes the 
  1771. computer pause for 7 seconds.
  1772.   If you want the computer to pause for 12 seconds instead of 7, 
  1773. retype line 20 so it says to mumble 12000 times instead of 7000.
  1774.  
  1775.   Although the typical computer mumbles 1000 times per second, 
  1776. your computer might mumble slightly faster or slower than that. 
  1777. Try the program on your computer, and notice how many seconds 
  1778. your computer pauses when doing 7000 mumbles. If your computer's 
  1779. typical, it pauses 7 seconds; if your computer mumbles quickly, 
  1780. it pauses less than 7 seconds; if your computer mumbles slowly, 
  1781. it pauses more than 7 seconds.
  1782.   Warning: your computer mumbles more slowly in long programs 
  1783. than in short ones.
  1784.   This program makes the computer print a famous joke:
  1785. 10 PRINT "YOUR TEETH ARE LIKE STARS"
  1786. 20 FOR I = 1 TO 7000: NEXT
  1787. 30 PRINT "THEY COME OUT AT NIGHT"
  1788. Line 30 is the punch line. Line 20 makes the computer pause 7 
  1789. seconds, before giving the punch line.
  1790.   Experiment: invent your own joke, and make the computer pause 
  1791. before printing the punch line.
  1792.   Eye test This program makes the computer test how fast you can 
  1793. read:
  1794. 10 CLS
  1795. 20 PRINT "IF YOU CAN READ THIS, YOU READ QUICKLY"
  1796. 30 FOR I = 1 TO 250: NEXT
  1797. 40 CLS
  1798.   Line 10 makes the computer clear the screen, so that the entire 
  1799. screen becomes blank. Line 20 makes the computer print:
  1800. IF YOU CAN READ THIS, YOU READ QUICKLY
  1801. Line 30 makes the computer pause for a quarter of a second (since 
  1802. 250 is a quarter of 1000). While the computer pauses, the message 
  1803. stays on the screen. Line 40 erases the screen, so the message 
  1804. stayed on the screen for just a quarter of a second. If you could 
  1805. read the message in a quarter of a second, you have very quick 
  1806. eyes!
  1807.   I can't read that quickly. Can you? Try it! To make the eye 
  1808. test easier, allow yourself longer than a quarter of a second to 
  1809. read the message, by changing the 250 to a larger number.
  1810.   Get together with your friends, and see how quickly they can 
  1811. read. Flash different messages on the screen by changing line 20. 
  1812. For example, try changing line 20 to this:
  1813. 20 PRINT "MUMBLING MORONS MAKE MY MOM MISS MURDER MYSTERIES 
  1814. MONDAY MORNING"
  1815. If your friends can read all that in a quarter of a second, they 
  1816. probably belong in the Guinness Book of World Records.
  1817.   To prevent your friends from cheating, make them close their 
  1818. eyes while you're typing the program. When you've finished typing 
  1819. the program, press the CLEAR key, so that the program becomes 
  1820. invisible. Then tell your friends to open their eyes and type 
  1821. RUN.
  1822.  
  1823.                       STEP
  1824.   The FOR statement can be varied:
  1825. Statement       Meaning
  1826. FOR I = 5 TO 17 STEP .1I will go from 5 to 17, counting by 
  1827. tenths.
  1828.                 So I will be 5, then 5.1, then 5.2, etc., up to 
  1829. 17.
  1830.  
  1831. FOR I = 5 TO 17 STEP 3I will be every third number from 5 to 17.
  1832.                 So I will be 5, then 8, then 11, then 14, then 
  1833. 17.
  1834.  
  1835. FOR I = 17 TO 5 STEP -3I will be every third number from 17 down 
  1836. to 5.
  1837.                 So I will be 17, then 14, then 11, then 8, then 
  1838. 5.
  1839.   To count down, you must use the word STEP. To count from 17 
  1840. down to 5, give this instruction:
  1841. FOR I = 17 TO 5 STEP -1
  1842.  
  1843.                                                      This program 
  1844. prints a rocket countdown:
  1845. 10 FOR I = 10 TO 1 STEP -1
  1846. 20     PRINT I
  1847. 30 NEXT
  1848. 40 PRINT "BLAST OFF!"
  1849. The computer will print:
  1850.  10
  1851.  9
  1852.  8
  1853.  7
  1854.  6
  1855.  5
  1856.  4
  1857.  3
  1858.  2
  1859.  1
  1860. BLAST OFF!
  1861.                                                      This 
  1862. statement is tricky:
  1863. FOR I = 5 TO 16 STEP 3
  1864. It says to start I at 5, and keep adding 3 until it gets past 16. 
  1865. So I will be 5, then 8, then 11, then 14. I won't be 17, since 17 
  1866. is past 16. The first value of I is 5; the last value is 14.
  1867.                                                      In the 
  1868. statement FOR I = 5 TO 16 STEP 3, the first value or initial 
  1869. value of I is 5, the limit value is 16, and the step size or 
  1870. increment is 3. The I is called the counter or index or 
  1871. loop-control variable. Although the limit value is 16, the last 
  1872. value or terminal value is 14.
  1873.                                                      Programmers 
  1874. usually say ``FOR I'', instead of ``FOR X'', because the letter I 
  1875. reminds them of the word index.
  1876.  
  1877.               VARIABLES & CONSTANTS
  1878.   A numeric constant is a simple number, such as:
  1879. 0     1     2     8     43.7     -524.6     .003
  1880. Another example of a numeric constant is 1.3E5, which means, 
  1881. ``take 1.3, and move its decimal point 5 places to the right''.
  1882.   A numeric constant does not contain any arithmetic. For 
  1883. example, since 7+1 contains arithmetic (+), it's not a numeric 
  1884. constant. 8 is a numeric constant, even though 7+1 isn't.
  1885.   A string constant is a simple string, in quotation marks:
  1886. "I LOVE YOU"     "76 TROMBONES"     "GO AWAY!!!"     "XYPW 
  1887. EXR///746"
  1888.   A constant is a numeric constant or a string constant:
  1889. 0     8     -524.6     1.3E5     "I LOVE YOU"     "XYPW 
  1890. EXR///746"
  1891.   A variable is something that stands for something else. If it 
  1892. stands for a string, it's called a string variable and ends with 
  1893. a dollar sign, like this:
  1894. A$     B$     Y$     Z$
  1895. If the variable stands for a number, it's called a numeric 
  1896. variable and lacks a dollar sign, like this:
  1897. A      B      Y      Z
  1898.   So all these are variables:
  1899. A$     B$     Y$     Z$     A     B     Y     Z
  1900.  
  1901.                    Expressions
  1902.   A numeric expression is a numeric constant (such as 8) or a 
  1903. numeric variable (such as A) or a combination of them, such as 
  1904. 8+Z, or 8*A, or Z*A, or 8*2, or 7+1, or even 
  1905. Z*A-(7+Z)/8+1.3E5*(-524.6+B).
  1906.   A string expression is a string constant (such as ``I LOVE 
  1907. YOU'') or a string variable (such as A$) or a combination.
  1908.   An expression is a numeric expression or a string expression.
  1909.  
  1910.                    Statements
  1911.   At the end of a GO TO statement, the line number must be a 
  1912. numeric constant.
  1913. Right:50 GO TO 100      (100 is a numeric constant.)
  1914. Wrong:50 GO TO N        (N is not a numeric constant.)
  1915.   The INPUT statement's prompt must be a string constant:
  1916. Right:10 INPUT "WHAT IS YOUR NAME";N$(``WHAT IS YOUR NAME'' is a 
  1917. constant.)
  1918. Wrong:10 INPUT Q$;N$    (Q$ is not a constant.)
  1919.   In a DATA statement, you must have constants.
  1920. Right:10 DATA 8, 1.3E5  (8 and 1.3E5 are constants.)
  1921. Wrong:10 DATA 7+1, 1.3E5(7+1 is not a constant.)
  1922. In the DATA statement, if the constant is a string, you can omit 
  1923. the quotation marks (unless the string contains a comma or a 
  1924. colon).
  1925. Right:10 DATA "JOE","MARY"
  1926. Also right:10 DATA JOE,MARY
  1927.   Here are the forms of the most popular BASIC statements:
  1928. General form                          Example
  1929. PRINT list of expressions             PRINT "THE TEMPERATURE 
  1930. IS";4+25;"DEGREES"
  1931. GO TO numeric constant                GO TO 10
  1932. END                                   END
  1933. STOP                                  STOP
  1934. variable = expression                 X = 47+2
  1935. INPUT string constant ; variable      INPUT "WHAT IS YOUR 
  1936. NAME";N$
  1937. IF condition THEN list of statements  IF A>=18 THEN PRINT "YOU": 
  1938. PRINT "VOTE"
  1939. DATA list of constants                DATA 
  1940. JOE,273,219,MARY,412,371
  1941. READ list of variables                READ N$,B,A
  1942. RESTORE                               RESTORE
  1943. FOR numeric variable =                FOR I = 59+1 TO 100+N STEP 
  1944. 2+3
  1945.     numeric expression TO
  1946.     numeric expression STEP
  1947.     numeric expression
  1948. NEXT numeric variable                 NEXT I
  1949.  
  1950.  
  1951.            LOOP TECHNIQUES
  1952.   Here's a strange program:
  1953. 10 A=5
  1954. 20 A=3+A
  1955. 30 PRINT A
  1956.   Line 20 means: the new A is 3 plus the old A. So the new A is 
  1957. 3+5, which is 8. Line 30 prints:
  1958.  8
  1959.   Let's look at that program more closely. Line 10 puts 5 into 
  1960. box A:
  1961.  
  1962. When the computer sees line 20, it examines the equation's right 
  1963. side and sees the 3+A. Since A is 5, the 3+A is 3+5, which is 8. 
  1964. So line 20 says: A=8. The computer puts 8 into box A:
  1965.  
  1966. Line 30 prints 8.
  1967.   Here's another weirdo:
  1968. 10 B=6
  1969. 20 B=B+1
  1970. 30 PRINT B*2
  1971. Line 20 says the new B is ``the old B plus 1''. So the new B is 
  1972. 6+1, which is 7. Line 30 prints:
  1973.  14
  1974.   In that program, line 10 says B is 6; but line 20 increases B, 
  1975. by adding 1 to B; so B becomes 7. Programmers say that B has been 
  1976. increased or incremented. In line 20, the ``1'' is called the 
  1977. increase or the increment.
  1978.   The opposite of ``increment'' is decrement:
  1979. 10 J=500
  1980. 20 J=J-1
  1981. 30 PRINT J
  1982. Line 10 says J starts at 500. But line 20 says the new J is ``the 
  1983. old J minus 1'', so the new J is 500-1, which is 499. Line 30 
  1984. prints:
  1985.  499
  1986. In that program, J was decreased (or decremented). In line 20, 
  1987. the ``1'' is called the decrease (or decrement).
  1988.                                                      Counting
  1989.                                          Suppose you want the 
  1990. computer to count, starting at 3, like this:
  1991.  3
  1992.  4
  1993.  5
  1994.  6
  1995.  7
  1996.  8
  1997.  etc.
  1998. This program does it, by a special technique:
  1999. 10 C=3
  2000. 20 PRINT C
  2001. 30 C=C+1
  2002. 40 GO TO 20
  2003.                                          In that program, C is 
  2004. called the counter, because it helps the computer count.
  2005.                                          Line 10 says C starts at 
  2006. 3. Line 20 makes the computer print C, so the computer prints:
  2007.  3
  2008.                                          Line 30 increases C by 
  2009. adding 1 to it, so C becomes 4. Line 40 sends the computer back 
  2010. to line 20, which prints the new value of C:
  2011.  4
  2012.                                          Then the computer comes 
  2013. to line 30 again, which increases C again so C becomes 5. Line 40 
  2014. sends the computer back to line 20 again, which prints:
  2015.  5
  2016.                                          The program's an 
  2017. infinite loop: the computer will print 3, 4, 5, 6, 7, 8, 9, 10, 
  2018. 11, 12, and so on, forever, unless you abort it.
  2019.                                          General procedure Here's 
  2020. the general procedure for making the computer count:
  2021. 1. Start C at some value (such as 3).
  2022. 2. Use C. (For example, tell the computer to PRINT C.)
  2023. 3. Increase C, by saying C=C+1.
  2024. 4. GO back TO step 2.
  2025.                                          Variations To read the 
  2026. printing more easily, put a semicolon at the end of the PRINT 
  2027. statement:
  2028. 10 C=3
  2029. 20 PRINT C;
  2030. 30 C=C+1
  2031. 40 GO TO 20
  2032. The semicolon makes the computer print horizontally:
  2033.  3  4  5  6  7  8  etc.
  2034.                                          This program makes the 
  2035. computer count, starting at 1:
  2036. 10 C=1
  2037. 20 PRINT C;
  2038. 30 C=C+1
  2039. 40 GO TO 20
  2040. The computer will print 1, 2, 3, 4, etc.
  2041.                                          This program makes the 
  2042. computer count, starting at 0:
  2043. 10 C=0
  2044. 20 PRINT C;
  2045. 30 C=C+1
  2046. 40 GO TO 20
  2047. The computer will print 0, 1, 2, 3, 4, etc.
  2048.                 Quiz
  2049.   Let's make the computer give this quiz:
  2050. What's the capital of Nevada?
  2051. What's the chemical symbol for iron?
  2052. What word means 'brother or sister'?
  2053. What was Beethoven's first name?
  2054. How many cups are in a quart?
  2055.   To make the computer score the quiz, we must tell it the 
  2056. correct answers, which are:
  2057. Carson City
  2058. Fe
  2059. sibling
  2060. Ludwig
  2061. 4
  2062.   So the program contains this data:
  2063. 10 DATA WHAT'S THE CAPITAL OF NEVADA,CARSON CITY
  2064. 20 DATA WHAT'S THE CHEMICAL SYMBOL FOR IRON,FE
  2065. 30 DATA WHAT WORD MEANS 'BROTHER OR SISTER',SIBLING
  2066. 40 DATA WHAT WAS BEETHOVEN'S FIRST NAME,LUDWIG
  2067. 50 DATA HOW MANY CUPS ARE IN A QUART,4
  2068.   Tell the computer to READ the data:
  2069. 100 READ Q$,A$
  2070. That line reads a pair of data: it reads a question (Q$) and the 
  2071. correct answer (A$).
  2072.   Make the computer ask the question and wait for the human's 
  2073. response:
  2074. 110 PRINT Q$;
  2075. 120 INPUT "??";H$
  2076. Line 110 prints the question. Line 120 prints question marks 
  2077. after the question, and waits for the human to respond; the 
  2078. human's response is called H$.
  2079.   Finally, evaluate the human's response. If the human's response 
  2080. (H$) is the correct answer (A$), make the computer say 
  2081. ``CORRECT'' and GO TO the next question:
  2082. 130 IF H$=A$ THEN PRINT "CORRECT": GO TO 100
  2083. But if the human's response is wrong, make the computer say 
  2084. ``NO'' and reveal the correct answer:
  2085. 140 PRINT "NO, THE ANSWER IS: ";A$: GO TO 100
  2086.   Here's a sample run:
  2087. RUN
  2088. WHAT'S THE CAPITAL OF NEVADA??? LAS VEGAS
  2089. NO, THE ANSWER IS: CARSON CITY
  2090. WHAT'S THE CHEMICAL SYMBOL FOR IRON??? FE
  2091. CORRECT
  2092. WHAT WORD MEANS 'BROTHER OR SISTER'??? I GIVE UP
  2093. NO, THE ANSWER IS: SIBLING
  2094. WHAT WAS BEETHOVEN'S FIRST NAME??? LUDVIG
  2095. NO, THE ANSWER IS: LUDWIG
  2096. HOW MANY CUPS ARE IN A QUART??? 4
  2097. CORRECT
  2098. OUT OF DATA
  2099.   To give a quiz about different topics, change the data in lines 
  2100. 10-50.
  2101.   Avoid OUT OF DATA Instead of making the computer say OUT OF 
  2102. DATA, let's make it say:
  2103. I HOPE YOU ENJOYED THE QUIZ
  2104.   To do that, write an end mark and an end routine:
  2105. 60 DATA END,END
  2106. 100 READ Q$,A$: IF Q$="END" THEN PRINT "I HOPE YOU ENJOYED T HE 
  2107. QUIZ": END
  2108.  
  2109.                                          Count the correct 
  2110. answers Let's make the computer count how many questions the 
  2111. human answered correctly. To do that, we need a counter. As 
  2112. usual, let's call it C:
  2113. 5 C=0
  2114. 10 DATA WHAT'S THE CAPITAL OF NEVADA,CARSON CITY
  2115. 20 DATA WHAT'S THE CHEMICAL SYMBOL FOR IRON,FE
  2116. 30 DATA WHAT WORD MEANS 'BROTHER OR SISTER',SIBLING
  2117. 40 DATA WHAT WAS BEETHOVEN'S FIRST NAME,LUDWIG
  2118. 50 DATA HOW MANY CUPS ARE IN A QUART,4
  2119. 60 DATA END,END
  2120. 100 READ Q$,A$: IF Q$="END" THEN PRINT "YOU ANSWERED";C;"OF  THE 
  2121. QUESTIONS CORRECTLY": PRINT "I HOPE YOU ENJOYED THE QUIZ ": END
  2122. 110 PRINT Q$;
  2123. 120 INPUT "??";H$
  2124. 130 IF H$=A$ THEN PRINT "CORRECT": C=C+1: GO TO 100
  2125. 140 PRINT "NO, THE ANSWER IS: ";A$: GO TO 100
  2126.                                          At the beginning of the 
  2127. program, the human hasn't answered any questions correctly yet, 
  2128. so line 5 begins the counter at 0. Each time the human answers a 
  2129. question correctly, line 130 increases the counter. When the 
  2130. program ends, line 100 prints the counter, by printing a message 
  2131. such as:
  2132. YOU ANSWERED 2 OF THE QUESTIONS CORRECTLY
  2133.                                          It would be nicer to 
  2134. print ___ 
  2135. YOU ANSWERED 2 OF THE 5 QUESTIONS CORRECTLY
  2136. YOUR SCORE IS 40 %
  2137. or, if the quiz were changed to include 8 questions:
  2138. YOU ANSWERED 2 OF THE 8 QUESTIONS CORRECTLY
  2139. YOUR SCORE IS 25 %
  2140. To make the computer print such a message, we must make the 
  2141. computer count how many questions were asked. So we need another 
  2142. counter. Since we already used C to count the number of correct 
  2143. answers, let's use Q to count the number of questions asked. Like 
  2144. C, Q must start at 0; and we must increase Q, by adding 1 each 
  2145. time another question is asked:
  2146. 5 C=0: Q=0
  2147. 10 DATA WHAT'S THE CAPITAL OF NEVADA,CARSON CITY
  2148. 20 DATA WHAT'S THE CHEMICAL SYMBOL FOR IRON,FE
  2149. 30 DATA WHAT WORD MEANS 'BROTHER OR SISTER',SIBLING
  2150. 40 DATA WHAT WAS BEETHOVEN'S FIRST NAME,LUDWIG
  2151. 50 DATA HOW MANY CUPS ARE IN A QUART,4
  2152. 60 DATA END,END
  2153. 100 READ Q$,A$: IF Q$="END" THEN PRINT "YOU ANSWERED";C;"OF 
  2154. THE";Q;"QUESTIONS CORRECTLY": PRINT "YOUR SCORE IS";C/Q*100;
  2155. "%": PRINT "I HOPE YOU ENJOYED THE QUIZ": END
  2156. 110 PRINT Q$;
  2157. 115 Q=Q+1
  2158. 120 INPUT "??";H$
  2159. 130 IF H$=A$ THEN PRINT "CORRECT": C=C+1: GO TO 100
  2160. 140 PRINT "NO, THE ANSWER IS: ";A$: GO TO 100
  2161.  
  2162.                      Summing
  2163.   Let's make the computer imitate an adding machine, so a run 
  2164. looks like this:
  2165. RUN
  2166. NOW THE SUM IS 0
  2167. WHAT NUMBER DO YOU WANT TO ADD TO THE SUM? 5
  2168. NOW THE SUM IS 5
  2169. WHAT NUMBER DO YOU WANT TO ADD TO THE SUM? 3
  2170. NOW THE SUM IS 8
  2171. WHAT NUMBER DO YOU WANT TO ADD TO THE SUM? 6.1
  2172. NOW THE SUM IS 14.1
  2173. WHAT NUMBER DO YOU WANT TO ADD TO THE SUM? -10
  2174. NOW THE SUM IS 4.1
  2175. etc.
  2176.   Here's the program:
  2177. 10 S=0
  2178. 20 PRINT "NOW THE SUM IS";S
  2179. 30 INPUT "WHAT NUMBER DO YOU WANT TO ADD TO THE SUM";X
  2180. 40 S=S+X
  2181. 50 GO TO 20
  2182.   Line 10 starts the sum at 0. Line 20 prints the sum. Line 30 
  2183. asks the human what number to add to the sum; the human's number 
  2184. is called X. Line 40 adds X to the sum, so the sum changes. Line 
  2185. 50 makes the computer go to line 20, which prints the new sum. 
  2186. Lines 20-50 form an infinite loop, which you must abort.
  2187.   Here's the general procedure for making the computer find a 
  2188. sum:
  2189. 1. Start S at 0.
  2190. 2. Use S. (For example, tell the computer to PRINT S.)
  2191. 3. Find out what number to add to S. (For example, let the human 
  2192. input an X.)
  2193. 4. Increase S, by saying S = S + the number to be added.
  2194. 5. GO back TO step 2.
  2195.  
  2196.                 Checking account
  2197.   If your bank's nasty, it charges you 10¢ to process each good 
  2198. check that you write, and a $5 penalty for each check that 
  2199. bounces; and it pays no interest on the money you've deposited.
  2200.   This program makes the computer imitate such a bank. . . . 
  2201. Start the sum at 0:10 S=0
  2202.  
  2203. Chat with the human:20 PRINT "YOUR CHECKING ACCOUNT CONTAINS";S
  2204.             30 INPUT "DEPOSIT OR WITHDRAW";A$
  2205.             40 IF A$="DEPOSIT" THEN GO TO 100
  2206.             50 IF A$="WITHDRAW" THEN GO TO 200
  2207.             60 PRINT "PLEASE SAY DEPOSIT OR WITHDRAW": GO TO 30
  2208.  
  2209. Deposit some money:100 INPUT "HOW MUCH DO YOU WANT TO DEPOSIT";D
  2210.             110 S=S+D
  2211.             120 GO TO 20
  2212.  
  2213. Withdraw some money:200 INPUT "HOW MUCH DO YOU WANT TO 
  2214. WITHDRAW";W
  2215.             210 W=W+.10
  2216.             220 IF W<=S THEN PRINT "OKAY": S=S-W: GO TO 20
  2217.             230 PRINT "THAT CHECK BOUNCED": S=S-5: GO TO 20
  2218.   In that program, the total amount of money in the checking 
  2219. account is called the sum. Line 10 starts that sum at 0. Line 20 
  2220. prints the sum. Line 30 asks whether the human wants to deposit 
  2221. or withdraw.
  2222.   If the human says DEPOSIT, the computer goes from line 40 to 
  2223. line 100 (which asks how much to deposit), then to line 110 
  2224. (which adds the deposit to the sum in the account), then back to 
  2225. line 20 (for the next transaction).
  2226.   But if the human says WITHDRAW instead of DEPOSIT, the computer 
  2227. goes from line 50 to line 200 (which asks how much to withdraw), 
  2228. then to line 210 (which adds the 10¢ service charge to the 
  2229. withdrawal amount). Then the computer reaches line 220, which 
  2230. checks whether the sum S in the account is large enough to cover 
  2231. the withdrawal (W). If W<=S, the computer says OKAY and processes 
  2232. the check, by subtracting W from the sum in the account. If W>S 
  2233. instead, the computer says THAT CHECK BOUNCED and decreases the 
  2234. sum in the account by the $5 penalty.
  2235.  
  2236.   How the program is nasty That program is nasty to customers. 
  2237. For example, suppose you have $1 in your account, and you try to 
  2238. write a check for 95¢. Since 95¢ + the 10¢ service charge = 
  2239. $1.05, which is more than you have in your account, your check 
  2240. will bounce, and you'll be penalized $5. That makes your balance 
  2241. will become negative $4, and the bank will demand that you pay 
  2242. the bank $4 ___ just because you wrote a check for 95¢!
  2243.   Another nuisance is when you leave town permanently and want to 
  2244. close your account. If your account contains $1, you can't get 
  2245. your dollar back! The most you can withdraw is 90¢, because 90¢ + 
  2246. the 10¢ service charge = $1.
  2247.   That nasty program makes customers hate the bank ___ and hate 
  2248. the computer!
  2249.   How to stop the nastiness The bank should make the program 
  2250. friendlier. Here's how.
  2251.   To stop accusing the customer of owing money, the bank should 
  2252. change any negative sum to 0:
  2253. 15 IF S<0 THEN S=0
  2254. To make sure the computer goes to that line, the bank's program 
  2255. should say GO TO 15 instead of GO TO 20; so the bank should 
  2256. change line 230 to this:
  2257. 230 PRINT "THAT CHECK BOUNCED": S=S-5: GO TO 15
  2258.   Also, to be friendly, the bank should ignore the 10¢ service 
  2259. charge when deciding whether a check will clear. So the bank 
  2260. should eliminate line 210. On the other hand, if the check does 
  2261. clear, the bank should impose the 10¢ service charge afterwards, 
  2262. like this:
  2263. 220 IF W<=S THEN PRINT "OKAY": S=S-W-.10: GO TO 15
  2264.   So if the bank is kind, it will insert line 15, use the new 
  2265. version of line 230, eliminate line 210, and use the new version 
  2266. of line 220.
  2267.   But some banks complain that those changes are too kind! For 
  2268. example, if a customer whose account contains just 1¢ writes a 
  2269. million-dollar check (which bounces), the new program charges him 
  2270. just 1¢ for the bad check; $5 might be more reasonable.
  2271.   Moral: the hardest thing about programming is choosing your 
  2272. goal ___ deciding what you want the computer to do.
  2273.  
  2274.                      Series
  2275.   Let's make the computer add together all the numbers from 7 to 
  2276. 100, so that the computer finds the sum of this series: 
  2277. 7+8+9+...+100. Here's how.
  2278. Start the Sum at 0:   10 S=0
  2279. Make I go from 7 to 100:20 FOR I = 7 TO 100
  2280. Increase the Sum, by adding each I to it:30     S=S+I
  2281.                       40 NEXT
  2282. Print the final Sum (which is 5029):50 PRINT S
  2283.   Let's make the computer add together the squares of all the 
  2284. numbers from 7 to 100, so that the computer finds the sum of this 
  2285. series: (7 squared) + (8 squared) + (9 squared) + . . . + (100 
  2286. squared). Here's how:
  2287. 10 S=0
  2288. 20 FOR I = 7 TO 100
  2289. 30     S=S+I*I
  2290. 40 NEXT
  2291. 50 PRINT S
  2292. It's the same as the previous program, except that line 30 says 
  2293. to add I*I instead of I. Line 50 prints the final sum, which is 
  2294. 338259.
  2295.                                                            Data sums
  2296.                                                      This program 
  2297. adds together the numbers in the data:
  2298. 10 S=0
  2299. 20 DATA 5, 3, 6.1, etc.
  2300. 30 DATA etc.
  2301. 40 DATA etc.
  2302. 50 DATA 0
  2303. 60 READ X: IF X=0 THEN PRINT S: END
  2304. 70 S=S+X
  2305. 80 GO TO 60
  2306.                                                      Line 10 
  2307. starts the sum at 0. Lines 20-40 contain the numbers to be added. 
  2308. The zero in line 50 is an end mark.
  2309.                                                      Line 60 
  2310. reads an X from the data. If X=0, the end of the data's been 
  2311. reached, so we want the computer to print the sum (S) and end. 
  2312. But if the X it reads is not zero, the computer proceeds from 
  2313. line 60 to line 70, adds X to the sum, and goes from line 80 to 
  2314. line 60, which reads another X.